<?php
class ShopController extends AppController {

	public $name = 'shop';
	public $isuservip = 'default';	
	public $vipviews = 'vip';		
	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('index','products','certificates','album','albumshow','news','newshow','contactus','join','credit','shopintro','productshow','goodcredit','goodrecord','shopfavorite','goodsfavorite','goodsaddcart');

	}
	
	public function index($shop_id) {
        $shop_id = intval($shop_id);
        if(!$shop_id) {
	        exit("非法请求！");
        }
		if(isset($this->my->id)){$user_id = $this->my->id; }else{ $user_id = 0 ;}
		$query = array(
			'conditions' => array('shopinfo.id' => $shop_id), 
			'fields' => array('shopinfo.*'), 
		);
        $SHOP = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);

        if(!$SHOP) { trigger_error("没有此商铺！");}//没有此商铺
		$SHOP = $SHOP['shopinfo'];		
        if($SHOP['lock_flg']) { trigger_error("商品不存在或商铺已锁定");}//锁定
        if($user_id!=$SHOP['user_id']){
	        if($SHOP['open_flg']) {
		        trigger_error("此商铺已关闭");
	        }//关闭
        }		
        if($user_id > 0){
            if($this->my->locked==1){
	            $this->C_session->destroy();
	            exit("您已经被锁定了");//非法操作
            }
        }
        //商铺分类
        $shop_rank = $SHOP['shop_categories'];			
		$shop_rank_arr = array();
        if($shop_rank) {
		    $cat = $shop_rank;
			$query = array(
				'fields' => array('shopcategories.*'), 
			);
	        $arr = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);				
            if($arr) {
			    $index = 0;
                $cats  = array();				
                while (1){
                    foreach ($arr AS $row){
                        if ($cat == $row['shopcategories']['id']){
                            $cat = $row['shopcategories']['parent_id'];
                            $cats[$index]['cat_id']   = $row['shopcategories']['id'];
                            $cats[$index]['cat_name'] = $row['shopcategories']['cat_name'];
                            $index++;
                            break;
                        }
                    }
                    if ($index == 0 || $cat == 0){
                        break;
                    }
                }				
                $shop_rank_arr = array_reverse($cats);				
			}			
		}		
        if ($shop_rank_arr){
	        $num = count($shop_rank_arr) - 1;
        }

		$query = array(
			'conditions' => array('user.id' => $SHOP['user_id']), 
			'fields' => array('user.*'), 
		);
        $ranks = POP::exe('[@@:UM user][@@:QU find:first]',$query);        		
        $SHOP['rank_id'] = $ranks['user']['rank_id'];		
		$query = array(
			'conditions' => array('userrank.id' => $SHOP['rank_id']), 
			'fields' => array('userrank.*'), 
		);
        $user_rank = POP::exe('[@@:UM userrank][@@:QU find:first]',$query);                	
        $user_rank = $user_rank['userrank'];	
        	
        $best_num = 8;
        $privilege = unserialize($user_rank['privilege']);

        if($privilege){
	        $best_num = $privilege[4];
        }
	    $header = array();
	    $header['title'] = "商铺首页 - ".$SHOP['shop_name'];
	    $header['keywords'] = $SHOP['shop_management'];
	    $header['description'] = strip_tags($SHOP['shop_intro']);
        /* 本页面信息处理 */

		$query = array(
            'conditions' => array('goods.shop_id'=>$shop_id,'goods.is_best'=>1,'goods.is_on_sale'=>1,'goods.lock_flg'=>0),
            'order'=>'sort_order asc,id desc',
            'limit'=>$best_num,
			'fields' => array('goods.*'), 
		);
        $best_goods = POP::exe('[@@:UM goods][@@:QU find:all]',$query);

		$query = array(
            'conditions' => array('goods.shop_id'=>$shop_id,'goods.is_promote'=>1,'goods.is_on_sale'=>1,'goods.lock_flg'=>0),
            'order'=>'sort_order asc,id desc',
            'limit'=>4,
			'fields' => array('goods.*'), 
		);
        $promote_goods = POP::exe('[@@:UM goods][@@:QU find:all]',$query);

        $page = intval(Request::get_args('page'));	
        $url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $tb = array('model' => 'shopguest',
				    'fields' => array('shopguest.*'),
					'conditions' => array('shopguest.shop_id'=>$shop_id,'shopguest.shop_del_status'=>1),
					'order' => 'shopguest.id desc',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
        $guestbook_list =  $this->fpage($tb); 
		
		$nav_selected="";
		$query = array(
            'conditions' => array('shopusercategorie.shop_id' => $shop_id),
            'order'=>'sort_order asc',
			'fields' => array('shopusercategorie.*'), 
		);
        $category_list = POP::exe('[@@:UM shopusercategorie][@@:QU find:all]',$query);
        
        $category_list_new = array();
        if(!empty($category_list)) {
	        foreach($category_list as $v) {
		        $category_list_new[$v['shopusercategorie']['id']]['id'] = $v['shopusercategorie']['id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_name'] = $v['shopusercategorie']['shop_cat_name'];
		        $category_list_new[$v['shopusercategorie']['id']]['parent_id'] = $v['shopusercategorie']['parent_id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_unit'] = $v['shopusercategorie']['shop_cat_unit'];
		        $category_list_new[$v['shopusercategorie']['id']]['sort_order'] = $v['shopusercategorie']['sort_order'];
	        }
        }		
        unset($category_list);
		$shop_customer = array();
		if(isset($this->my->username) && $user_id!=$SHOP['user_id']) {
			$query = array(
					'joins' => array(
					array('type'=>'left','table'=>'shopcustomcat','conditions'=>array('shopcustomer.customcat_id' =>'shopcustomcat.id')),					
					),
					'fields' => array('shopcustomcat.custom_cat_name','shopcustomcat.rebate'),
					'conditions' => array('shopcustomer.shop_id' =>$shop_id,'shopcustomer.user_name' =>$this->my->username),
				);	
	        $shop_customer=POP::exe('[@@:UM shopcustomer][@@:QU find:first]',$query);	        
			if(isset($shop_customer['shopcustomcat'])) {
			    $shop_customer=$shop_customer['shopcustomcat'];
			}
		}
		
		$query = array(
				'joins' => array(
				array('type'=>'left','table'=>'userrank','conditions'=>array('user.rank_id' =>'userrank.id')),					
				),
				'fields' => array('userrank.rank_name'),
				'conditions' => array('user.id' =>$shop_id),
			);	
	    $rank_name = POP::exe('[@@:UM user][@@:QU find:first]',$query);

        //获取商家信用值
		$query = array(
            'conditions' => array('credit.seller' => $shop_id),
			'fields' => array('credit.seller_credit'),
			'calculate'=>'sum' 
		);
	    $credit = POP::exe('[@@:UM credit][@@:QU find:calculate]',$query);
        $credit['SUM(seller_credit)'] = intval($credit['sum.credit.seller_credit']);
        if( $credit['SUM(seller_credit)'] < 0)$credit['SUM(seller_credit)']=0;			

		$query = array(
            'conditions' => array('integral.int_min <'=>($credit['SUM(seller_credit)']+1),'integral.int_max >'=>($credit['SUM(seller_credit)']-1)),
			'fields' => array('integral.*'), 
		);
        $credit_row = $integral = POP::exe('[@@:UM integral][@@:QU find:first]',$query);



	    if($integral){
            $integral = $integral['integral']['int_grade'];
        }else{
            $integral = 0;
        }		
        $seller_credit = $credit['SUM(seller_credit)'];	

		$query = array(
            'conditions' => array('shoprequest.user_id' => $shop_id),
			'fields' => array('shoprequest.*'), 
		);
        $SHOPREQUEST = POP::exe('[@@:UM shoprequest][@@:QU find:first]',$query);
        
		$SHOPREQUEST = $SHOPREQUEST['shoprequest'];			
	    $this->set('SHOPREQUEST',$SHOPREQUEST);
	    
		$query = array(
            'conditions' => array('shoplinks.shop_id'=>$shop_id,'shoplinks.is_show'=>1),
            'order'=>'id desc',
            'limit'=>10,
			'fields' => array('shoplinks.*'), 
		);
        $shoplinks = POP::exe('[@@:UM shoplinks][@@:QU find:all]',$query);
	
        $this->set('shoplinks',$shoplinks);	
        $this->set('category_list_new',$category_list_new);	
	    $this->set('header',$header);
	    $this->set('best_goods',$best_goods);
	    $this->set('promote_goods',$promote_goods);
	    $this->set('shop_customer',$shop_customer);
	    $this->set('guestbook_list',$guestbook_list);
	    $this->set('SHOP',$SHOP);
	    $this->set('shop_rank_arr',$shop_rank_arr);		
        if(empty($SHOP['shop_template'])){
		    $SHOP['shop_template'] = $this->isuservip ;
		}
        if(isset($privilege[11]) && $privilege[11]>0 ){
		    $SHOP['shop_template'] = $this->vipviews ;
		}		
		if($SHOP['shop_template']== $this->isuservip) {
		    $this->render('index');	
		}else {
		    $this->render($SHOP['shop_template'].'/index');		
		}	    	
	}	

	public function products($shop_id) {
		if(!isset(Request::$query['page']))Request::$query['page']=1;
		if(!isset(Request::$query['cat']))Request::$query['cat']=0;
		if(!isset(Request::$query['k']))Request::$query['k']="";
		if(!isset(Request::$query['pr1']))Request::$query['pr1']="";		
		if(!isset(Request::$query['pr2']))Request::$query['pr2']="";
		
        $up_price = intval(Request::get_args("up_price"));		
        $down_price = intval(Request::get_args("down_price"));		
        $pv = intval(Request::get_args("pv"));
        $fpv = intval(Request::get_args("fpv"));
        $new = intval(Request::get_args("new"));
		
        $pr1 = Request::$query['pr1'];
        $pr2 = Request::$query['pr2'];		
        $k = Request::$query['k'];		
        $shop_cat_id = intval(Request::$query['cat']);		
        $page = intval(Request::$query['page']);	
        $shop_id = intval($shop_id);
        if(!$shop_id) {
	        exit("非法请求！");
        }
		if(isset($this->my->id)){$user_id = $this->my->id; }else{ $user_id = 0 ;}
		$query = array(
			'conditions' => array('shopinfo.id' => $shop_id), 
			'fields' => array('shopinfo.*'), 
		);
        $SHOP = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);		
        if(!$SHOP) { trigger_error("没有此商铺！");}//没有此商铺
		$SHOP = $SHOP['shopinfo'];		
        if($SHOP['lock_flg']) { trigger_error("商品不存在或商铺已锁定");}//锁定
        if($user_id!=$SHOP['user_id']){
	        if($SHOP['open_flg']) {
		        trigger_error("此商铺已关闭");
	        }//关闭
        }		
        if($user_id > 0){
            if($this->my->locked==1){
	            $this->C_session->destroy();
	            exit("您已经被锁定了");//非法操作
            }
        }
        //商铺分类
        $shop_rank = $SHOP['shop_categories'];			
		$shop_rank_arr = array();
        if($shop_rank) {
		    $cat = $shop_rank;	
			$query = array(
				'fields' => array('shopcategories.*'), 
			);
	        $arr = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);			
            if($arr) {
			    $index = 0;
                $cats  = array();				
                while (1){
                    foreach ($arr AS $row){
                        if ($cat == $row['shopcategories']['id']){
                            $cat = $row['shopcategories']['parent_id'];
                            $cats[$index]['cat_id']   = $row['shopcategories']['id'];
                            $cats[$index]['cat_name'] = $row['shopcategories']['cat_name'];
                            $index++;
                            break;
                        }
                    }
                    if ($index == 0 || $cat == 0){
                        break;
                    }
                }				
                $shop_rank_arr = array_reverse($cats);				
			}			
		}		
        if ($shop_rank_arr){
	        $num = count($shop_rank_arr) - 1;
        }	 

		$query = array(
			'conditions' => array('user.id' => $SHOP['user_id']), 
			'fields' => array('user.*'), 
		);
        $ranks = POP::exe('[@@:UM user][@@:QU find:first]',$query);        		
        $SHOP['rank_id'] = $ranks['user']['rank_id'];		
		$query = array(
			'conditions' => array('userrank.id' => $SHOP['rank_id']), 
			'fields' => array('userrank.*'), 
		);
        $user_rank = POP::exe('[@@:UM userrank][@@:QU find:first]',$query);                	
        $user_rank = $user_rank['userrank'];
		
        $best_num = 8;
        $privilege = unserialize($user_rank['privilege']);
        if($privilege){
	        $best_num = $privilege[4];
        }
	    $header = array();
	    $header['title'] = "商品展示 - ".$SHOP['shop_name'];
	    $header['keywords'] = $SHOP['shop_management'];
	    $header['description'] = strip_tags($SHOP['shop_intro']);	

		$conditions = array();
		$conditions['goods.shop_id']= $shop_id;
		$conditions['goods.is_on_sale']= 1;
	
        if($pr1 && $pr2) {
		    $conditions['goods.goods_price >']= $pr1;
		    $conditions['goods.goods_price <']= $pr2;		    	        
        }		
        if( $shop_cat_id ) {
			$query = array(
	            'conditions' => array('shopusercategorie.id' => $shop_cat_id),
				'fields' => array('shopusercategorie.*'), 
			);
            $catinfo = POP::exe('[@@:UM shopusercategorie][@@:QU find:first]',$query);

            if(!$catinfo && $shop_id) {
	            $catinfo['shop_id'] = $shop_id;
	            $catinfo['shop_cat_name'] ="搜索";
            }
			$catinfo = $catinfo['shopusercategorie'];
			$ids = array();
            $ids[$shop_cat_id] = $shop_cat_id;
			$query = array(
	            'conditions' => array('shopusercategorie.parent_id' => $shop_cat_id),
				'fields' => array('shopusercategorie.*'), 
			);
			$rows = POP::exe('[@@:UM shopusercategorie][@@:QU find:all]',$query);
						
            if($rows) {
	            foreach($rows as $v){
		            $ids[$v['shopusercategorie']['id']]= $v['shopusercategorie']['id'];
	            }
            }
		    $conditions['goods.ucat_id']= $ids;            
		}
        if($k) {		
            //$goods_sql = " and ucat_id=$shop_cat_id ";			
            $ks = explode(' ', $k);
            $k_conditions = array();
            foreach($ks as $key) {
                if (!empty($key)){
                    $k_conditions['goods.goods_name like'] = "%$key%";
                    $k_conditions['goods.keyword like'] = "%$key%";
                }
            }
            if (!empty($k_conditions)) {
	            $tk_conditions['or'] = $k_conditions;
		        $conditions['and']= $tk_conditions;
            }		
        } 			

		$query = array(
            'conditions' => array('shopusercategorie.shop_id' => $shop_id),
            'order'=>'sort_order asc',
			'fields' => array('shopusercategorie.*'), 
		);
        $category_list = POP::exe('[@@:UM shopusercategorie][@@:QU find:all]',$query);


        $category_list_new = array();
        if(!empty($category_list)) {
	        foreach($category_list as $v) {
		        $category_list_new[$v['shopusercategorie']['id']]['id'] = $v['shopusercategorie']['id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_name'] = $v['shopusercategorie']['shop_cat_name'];
		        $category_list_new[$v['shopusercategorie']['id']]['parent_id'] = $v['shopusercategorie']['parent_id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_unit'] = $v['shopusercategorie']['shop_cat_unit'];
		        $category_list_new[$v['shopusercategorie']['id']]['sort_order'] = $v['shopusercategorie']['sort_order'];
	        }
        }		
        unset($category_list);
		$shop_customer = array();
		if(isset($this->my->username) && $user_id!=$SHOP['user_id']) {
			$query = array(
					'joins' => array(
					array('type'=>'left','table'=>'shopcustomcat','conditions'=>array('shopcustomer.customcat_id' =>'shopcustomcat.id')),					
					),
					'fields' => array('shopcustomcat.custom_cat_name','shopcustomcat.rebate'),
					'conditions' => array('shopcustomer.shop_id' =>$shop_id,'shopcustomer.user_name' =>$this->my->username),
				);	
	        $shop_customer=POP::exe('[@@:UM shopcustomer][@@:QU find:first]',$query);	        
			if(isset($shop_customer['shopcustomcat'])) {
			    $shop_customer=$shop_customer['shopcustomcat'];
			}			
		}
				
		$url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		$tb = array('model' => 'goods',
					'fields' => array("goods.id","goods.goods_name","goods.goods_price","goods.goods_thumb","goods.is_set_image","goods.transport_template_price","goods.goods_refer_price","goods.goods_number"),
					'conditions' => $conditions,
					'order' => 'sort_order asc,id desc',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				);	
	    if ($up_price) {
		    $tb['order']=" goods_price asc ";
	    }	
	    if ($down_price) {
		    $tb['order']=" goods_price desc ";
	    }		
	    if ($pv) {
		    $tb['order']=" pv desc ";
	    }	
	    if ($fpv) {
		    $tb['order']=" favpv desc ";
	    }			
	    if ($new) {
		    $tb['order']=" id desc ";
	    }			
		$result =  $this->fpage($tb);	
		
		$query = array(
            'conditions' => array('shoprequest.user_id' => $shop_id),
			'fields' => array('shoprequest.*'), 
		);
        $SHOPREQUEST = POP::exe('[@@:UM shoprequest][@@:QU find:first]',$query);        
		$SHOPREQUEST = $SHOPREQUEST['shoprequest'];			
	    $this->set('SHOPREQUEST',$SHOPREQUEST);	
		
        $this->set('category_list_new',$category_list_new);	
	    $this->set('header',$header);
	    $this->set('SHOP',$SHOP);
	    $this->set('shop_rank_arr',$shop_rank_arr);	
	    $this->set('shop_customer',$shop_customer);		
	    $this->set('result',$result);
	    $this->set('keyworld',$k);	
	    $this->set('k',$k);			
	    $this->set('cat',$shop_cat_id);	
	    $this->set('pr1',$pr1);			
	    $this->set('pr2',$pr2);	
        if(empty($SHOP['shop_template'])){
		    $SHOP['shop_template'] = $this->isuservip ;
		}
        if(isset($privilege[11]) && $privilege[11]>0 ){
		    $SHOP['shop_template'] = $this->vipviews ;
		}		
		if($SHOP['shop_template']== $this->isuservip) {
		    $this->render('products');	
		}else {
		    $this->render($SHOP['shop_template'].'/products');		
		}		

        
	}
	public function certificates($shop_id) {
        $shop_id = intval($shop_id);
        if(!$shop_id) {
	        exit("非法请求！");
        }
		if(isset($this->my->id)){$user_id = $this->my->id; }else{ $user_id = 0 ;}
		$query = array(
			'conditions' => array('shopinfo.id' => $shop_id), 
			'fields' => array('shopinfo.*'), 
		);
        $SHOP = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);		
        if(!$SHOP) { trigger_error("没有此商铺！");}//没有此商铺
		$SHOP = $SHOP['shopinfo'];		
        if($SHOP['lock_flg']) { trigger_error("商品不存在或商铺已锁定");}//锁定
        if($user_id!=$SHOP['user_id']){
	        if($SHOP['open_flg']) {
		        trigger_error("此商铺已关闭");
	        }//关闭
        }		
        if($user_id > 0){
            if($this->my->locked==1){
	            $this->C_session->destroy();
	            exit("您已经被锁定了");//非法操作
            }
        }
        //商铺分类
        $shop_rank = $SHOP['shop_categories'];			
		$shop_rank_arr = array();
        if($shop_rank) {
		    $cat = $shop_rank;
			$query = array(
				'fields' => array('shopcategories.*'), 
			);
	        $arr = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);			
            if($arr) {
			    $index = 0;
                $cats  = array();				
                while (1){
                    foreach ($arr AS $row){
                        if ($cat == $row['shopcategories']['id']){
                            $cat = $row['shopcategories']['parent_id'];
                            $cats[$index]['cat_id']   = $row['shopcategories']['id'];
                            $cats[$index]['cat_name'] = $row['shopcategories']['cat_name'];
                            $index++;
                            break;
                        }
                    }
                    if ($index == 0 || $cat == 0){
                        break;
                    }
                }				
                $shop_rank_arr = array_reverse($cats);				
			}			
		}		
        if ($shop_rank_arr){
	        $num = count($shop_rank_arr) - 1;
        }

		$query = array(
			'conditions' => array('user.id' => $SHOP['user_id']), 
			'fields' => array('user.*'), 
		);
        $ranks = POP::exe('[@@:UM user][@@:QU find:first]',$query);        		
        $SHOP['rank_id'] = $ranks['user']['rank_id'];		
		$query = array(
			'conditions' => array('userrank.id' => $SHOP['rank_id']), 
			'fields' => array('userrank.*'), 
		);
        $user_rank = POP::exe('[@@:UM userrank][@@:QU find:first]',$query);                	
        $user_rank = $user_rank['userrank'];
		
        $best_num = 8;
        $privilege = unserialize($user_rank['privilege']);
        if($privilege){
	        $best_num = $privilege[4];
        }
	    $header = array();
	    $header['title'] = "商铺证件 - ".$SHOP['shop_name'];
	    $header['keywords'] = $SHOP['shop_management'];
	    $header['description'] = strip_tags($SHOP['shop_intro']);	

		$query = array(
            'conditions' => array('shoprequest.user_id' => $shop_id),
			'fields' => array('shoprequest.*'), 
		);
        $SHOPREQUEST = POP::exe('[@@:UM shoprequest][@@:QU find:first]',$query);  
		$SHOPREQUEST = $SHOPREQUEST['shoprequest'];			
	
        $nav_selected="";
		$query = array(
            'conditions' => array('shopusercategorie.shop_id' => $shop_id),
            'order'=>'sort_order asc',
			'fields' => array('shopusercategorie.*'), 
		);
        $category_list = POP::exe('[@@:UM shopusercategorie][@@:QU find:all]',$query);



        $category_list_new = array();
        if(!empty($category_list)) {
	        foreach($category_list as $v) {
		        $category_list_new[$v['shopusercategorie']['id']]['id'] = $v['shopusercategorie']['id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_name'] = $v['shopusercategorie']['shop_cat_name'];
		        $category_list_new[$v['shopusercategorie']['id']]['parent_id'] = $v['shopusercategorie']['parent_id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_unit'] = $v['shopusercategorie']['shop_cat_unit'];
		        $category_list_new[$v['shopusercategorie']['id']]['sort_order'] = $v['shopusercategorie']['sort_order'];
	        }
        }		
        unset($category_list);
		
		$query = array(
				'joins' => array(
				array('type'=>'left','table'=>'userrank','conditions'=>array('user.rank_id' =>'userrank.id')),					
				),
				'fields' => array('userrank.rank_name'),
				'conditions' => array('user.id' =>$shop_id),
			);	
	    $rank_name = POP::exe('[@@:UM user][@@:QU find:first]',$query);



        //获取商家信用值
		$query = array(
            'conditions' => array('credit.seller' => $shop_id),
			'fields' => array('credit.seller_credit'),
			'calculate'=>'sum' 
		);
	    $credit = POP::exe('[@@:UM credit][@@:QU find:calculate]',$query);
	    
        $credit['SUM(seller_credit)'] = intval($credit['sum.credit.seller_credit']);
        if( $credit['SUM(seller_credit)'] < 0)$credit['SUM(seller_credit)']=0;			

		$query = array(
            'conditions' => array('integral.int_min <'=>($credit['SUM(seller_credit)']+1),'integral.int_max >'=>($credit['SUM(seller_credit)']-1)),
			'fields' => array('integral.*'), 
		);
        $credit_row = $integral = POP::exe('[@@:UM integral][@@:QU find:first]',$query);

	    if($integral){
            $integral = $integral['integral']['int_grade'];
        }else{
            $integral = 0;
        }		
        $seller_credit = $credit['SUM(seller_credit)'];		

		$query = array(
            'conditions' => array('shoprequest.user_id' => $shop_id),
			'fields' => array('shoprequest.*'), 
		);
        $SHOPREQUEST = POP::exe('[@@:UM shoprequest][@@:QU find:first]',$query); 		
		$SHOPREQUEST = $SHOPREQUEST['shoprequest'];			
	    $this->set('SHOPREQUEST',$SHOPREQUEST);			
		
        $this->set('category_list_new',$category_list_new);	
	    $this->set('header',$header);
	    $this->set('SHOP',$SHOP);
	    $this->set('shop_rank_arr',$shop_rank_arr);		
	    $this->set('SHOPREQUEST',$SHOPREQUEST);	
        if(empty($SHOP['shop_template'])){
		    $SHOP['shop_template'] = $this->isuservip ;
		}	
        if(isset($privilege[11]) && $privilege[11]>0 ){
		    $SHOP['shop_template'] = $this->vipviews ;
		}		
		if($SHOP['shop_template']== $this->isuservip) {
		    $this->render('certificates');	
		}else {
		    $this->render($SHOP['shop_template'].'/certificates');		
		}		
        
	}
	
	public function album($shop_id) {
        $shop_id = intval($shop_id);	
        if(!$shop_id) {
	        exit("非法请求！");
        }
		if(isset($this->my->id)){$user_id = $this->my->id; }else{ $user_id = 0 ;}
		$query = array(
			'conditions' => array('shopinfo.id' => $shop_id), 
			'fields' => array('shopinfo.*'), 
		);
        $SHOP = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);		
        if(!$SHOP) { trigger_error("没有此商铺！");}//没有此商铺
		$SHOP = $SHOP['shopinfo'];		
        if($SHOP['lock_flg']) { trigger_error("商品不存在或商铺已锁定");}//锁定
        if($user_id!=$SHOP['user_id']){
	        if($SHOP['open_flg']) {
		        trigger_error("此商铺已关闭");
	        }//关闭
        }		
        if($user_id > 0){
            if($this->my->locked==1){
	            $this->C_session->destroy();
	            exit("您已经被锁定了");//非法操作
            }
        }
        //商铺分类
        $shop_rank = $SHOP['shop_categories'];			
		$shop_rank_arr = array();
        if($shop_rank) {
		    $cat = $shop_rank;
			$query = array(
				'fields' => array('shopcategories.*'), 
			);
	        $arr = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);
			
            if($arr) {
			    $index = 0;
                $cats  = array();				
                while (1){
                    foreach ($arr AS $row){
                        if ($cat == $row['shopcategories']['id']){
                            $cat = $row['shopcategories']['parent_id'];
                            $cats[$index]['cat_id']   = $row['shopcategories']['id'];
                            $cats[$index]['cat_name'] = $row['shopcategories']['cat_name'];
                            $index++;
                            break;
                        }
                    }
                    if ($index == 0 || $cat == 0){
                        break;
                    }
                }				
                $shop_rank_arr = array_reverse($cats);				
			}			
		}		
        if ($shop_rank_arr){
	        $num = count($shop_rank_arr) - 1;
        }	

		$query = array(
			'conditions' => array('user.id' => $SHOP['user_id']), 
			'fields' => array('user.*'), 
		);
        $ranks = POP::exe('[@@:UM user][@@:QU find:first]',$query);        		
        $SHOP['rank_id'] = $ranks['user']['rank_id'];		
		$query = array(
			'conditions' => array('userrank.id' => $SHOP['rank_id']), 
			'fields' => array('userrank.*'), 
		);
        $user_rank = POP::exe('[@@:UM userrank][@@:QU find:first]',$query);                	
        $user_rank = $user_rank['userrank'];
		
        $best_num = 8;
        $privilege = unserialize($user_rank['privilege']);
        if($privilege){
	        $best_num = $privilege[4];
        }
	    $header = array();
	    $header['title'] = "商铺联系我们 - ".$SHOP['shop_name'];
	    $header['keywords'] = $SHOP['shop_management'];
	    $header['description'] = strip_tags($SHOP['shop_intro']);	

        $nav_selected="";

		$query = array(
            'conditions' => array('shopusercategorie.shop_id' => $shop_id),
            'order'=>'sort_order asc',
			'fields' => array('shopusercategorie.*'), 
		);
        $category_list = POP::exe('[@@:UM shopusercategorie][@@:QU find:all]',$query);


        $category_list_new = array();
        if(!empty($category_list)) {
	        foreach($category_list as $v) {
		        $category_list_new[$v['shopusercategorie']['id']]['id'] = $v['shopusercategorie']['id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_name'] = $v['shopusercategorie']['shop_cat_name'];
		        $category_list_new[$v['shopusercategorie']['id']]['parent_id'] = $v['shopusercategorie']['parent_id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_unit'] = $v['shopusercategorie']['shop_cat_unit'];
		        $category_list_new[$v['shopusercategorie']['id']]['sort_order'] = $v['shopusercategorie']['sort_order'];
	        }
        }		
        unset($category_list);
		
		$query = array(
				'joins' => array(
				array('type'=>'left','table'=>'userrank','conditions'=>array('user.rank_id' =>'userrank.id')),					
				),
				'fields' => array('userrank.rank_name'),
				'conditions' => array('user.id' =>$shop_id),
			);	
	    $rank_name = POP::exe('[@@:UM user][@@:QU find:first]',$query);
	    
        //获取商家信用值
		$query = array(
            'conditions' => array('credit.seller' => $shop_id),
			'fields' => array('credit.seller_credit'),
			'calculate'=>'sum' 
		);
	    $credit = POP::exe('[@@:UM credit][@@:QU find:calculate]',$query);
        $credit['SUM(seller_credit)'] = intval($credit['sum.credit.seller_credit']);	    
        if( $credit['SUM(seller_credit)'] < 0)$credit['SUM(seller_credit)']=0;			

		$query = array(
            'conditions' => array('integral.int_min <'=>($credit['SUM(seller_credit)']+1),'integral.int_max >'=>($credit['SUM(seller_credit)']-1)),
			'fields' => array('integral.*'), 
		);
        $credit_row = $integral = POP::exe('[@@:UM integral][@@:QU find:first]',$query);

	    if($integral){
            $integral = $integral['integral']['int_grade'];
        }else{
            $integral = 0;
        }		
        $seller_credit = $credit['SUM(seller_credit)'];		
		
        $this->set('category_list_new',$category_list_new);	
	    $this->set('header',$header);
	    $this->set('SHOP',$SHOP);
	    $this->set('shop_rank_arr',$shop_rank_arr);		

		$query = array(
            'conditions' =>array('shopalbumcategory.shop_id' => $shop_id),
            'order' => 'sort_order asc',
			'fields' => array('shopalbumcategory.*'), 
		);
        $shop_album_category = POP::exe('[@@:UM shopalbumcategory][@@:QU find:all]',$query);
			
	    $album_ids = array();
		$shop_album_cat = array();
		$shop_album_cat[0]= "临时图片夹";
		foreach($shop_album_category as $key => $value) {
		    $album_ids[$value['shopalbumcategory']['id']] = $value['shopalbumcategory']['id'];
			$shop_album_cat[$value['shopalbumcategory']['id']] =  $value['shopalbumcategory']['album_cat_name'];
		}

		$query = array(
            'conditions' => array('imgsize.shopalbum_id' => $album_ids,'imgsize.uid' => $shop_id),
			'fields' => array('imgsize.*'),
			'group' => 'shopalbum_id asc',
			'order' => 'id asc' 
		);
	    $shop_album = POP::exe('[@@:UM imgsize][@@:QU find:all]',$query);
	     
		$query = array(
            'conditions' => array('shoprequest.user_id' => $shop_id),
			'fields' => array('shoprequest.*'), 
		);
        $SHOPREQUEST = POP::exe('[@@:UM shoprequest][@@:QU find:first]',$query); 
		$SHOPREQUEST = $SHOPREQUEST['shoprequest'];			
	    $this->set('SHOPREQUEST',$SHOPREQUEST);			 
		 
	    $this->set('shop_album_cat',$shop_album_cat);
	    $this->set('shop_album',$shop_album);		
	    $this->set('user_id',$user_id);               		
        if(empty($SHOP['shop_template'])){
		    $SHOP['shop_template'] = $this->isuservip ;
		}	
        if(isset($privilege[11]) && $privilege[11]>0 ){
		    $SHOP['shop_template'] = $this->vipviews ;
		}		
		if($SHOP['shop_template']== $this->isuservip) {
		    $this->render('album');	
		}else {
		    $this->render($SHOP['shop_template'].'/album');		
		}		

        
	}
	
	
	public function albumshow($shop_id) {
        $shop_id = intval($shop_id);
        $shopalbum_id = intval(Request::get_args('cat'));		
        if(!$shop_id) {
	        exit("非法请求！");
        }
		if(isset($this->my->id)){$user_id = $this->my->id; }else{ $user_id = 0 ;}
		$query = array(
			'conditions' => array('shopinfo.id' => $shop_id), 
			'fields' => array('shopinfo.*'), 
		);
        $SHOP = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);		
        if(!$SHOP) { trigger_error("没有此商铺！");}//没有此商铺
		$SHOP = $SHOP['shopinfo'];		
        if($SHOP['lock_flg']) { trigger_error("商品不存在或商铺已锁定");}//锁定
        if($user_id!=$SHOP['user_id']){
	        if($SHOP['open_flg']) {
		        trigger_error("此商铺已关闭");
	        }//关闭
        }		
        if($user_id > 0){
            if($this->my->locked==1){
	            $this->C_session->destroy();
	            exit("您已经被锁定了");//非法操作
            }
        }
        //商铺分类
        $shop_rank = $SHOP['shop_categories'];			
		$shop_rank_arr = array();
        if($shop_rank) {
		    $cat = $shop_rank;
			$query = array(
				'fields' => array('shopcategories.*'), 
			);
	        $arr = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);
            if($arr) {
			    $index = 0;
                $cats  = array();				
                while (1){
                    foreach ($arr AS $row){
                        if ($cat == $row['shopcategories']['id']){
                            $cat = $row['shopcategories']['parent_id'];
                            $cats[$index]['cat_id']   = $row['shopcategories']['id'];
                            $cats[$index]['cat_name'] = $row['shopcategories']['cat_name'];
                            $index++;
                            break;
                        }
                    }
                    if ($index == 0 || $cat == 0){
                        break;
                    }
                }				
                $shop_rank_arr = array_reverse($cats);				
			}			
		}		
        if ($shop_rank_arr){
	        $num = count($shop_rank_arr) - 1;
        }		

		$query = array(
			'conditions' => array('user.id' => $SHOP['user_id']), 
			'fields' => array('user.*'), 
		);
        $ranks = POP::exe('[@@:UM user][@@:QU find:first]',$query);        		
        $SHOP['rank_id'] = $ranks['user']['rank_id'];		
		$query = array(
			'conditions' => array('userrank.id' => $SHOP['rank_id']), 
			'fields' => array('userrank.*'), 
		);
        $user_rank = POP::exe('[@@:UM userrank][@@:QU find:first]',$query);                	
        $user_rank = $user_rank['userrank'];
		
        $best_num = 8;
        $privilege = unserialize($user_rank['privilege']);
        if($privilege){
	        $best_num = $privilege[4];
        }
	    $header = array();
	    $header['title'] = "商铺联系我们 - ".$SHOP['shop_name'];
	    $header['keywords'] = $SHOP['shop_management'];
	    $header['description'] = strip_tags($SHOP['shop_intro']);	

        $nav_selected="";
		$query = array(
            'conditions' => array('shopusercategorie.shop_id' => $shop_id),
            'order'=>'sort_order asc',
			'fields' => array('shopusercategorie.*'), 
		);
        $category_list = POP::exe('[@@:UM shopusercategorie][@@:QU find:all]',$query);

        $category_list_new = array();
        if(!empty($category_list)) {
	        foreach($category_list as $v) {
		        $category_list_new[$v['shopusercategorie']['id']]['id'] = $v['shopusercategorie']['id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_name'] = $v['shopusercategorie']['shop_cat_name'];
		        $category_list_new[$v['shopusercategorie']['id']]['parent_id'] = $v['shopusercategorie']['parent_id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_unit'] = $v['shopusercategorie']['shop_cat_unit'];
		        $category_list_new[$v['shopusercategorie']['id']]['sort_order'] = $v['shopusercategorie']['sort_order'];
	        }
        }		
        unset($category_list);
		
		$query = array(
				'joins' => array(
				array('type'=>'left','table'=>'userrank','conditions'=>array('user.rank_id' =>'userrank.id')),					
				),
				'fields' => array('userrank.rank_name'),
				'conditions' => array('user.id' =>$shop_id),
			);	
	    $rank_name = POP::exe('[@@:UM user][@@:QU find:first]',$query);

        //获取商家信用值
		$query = array(
            'conditions' => array('credit.seller' => $shop_id),
			'fields' => array('credit.seller_credit'),
			'calculate'=>'sum' 
		);
	    $credit = POP::exe('[@@:UM credit][@@:QU find:calculate]',$query);
	    
        $credit['SUM(seller_credit)'] = intval($credit[0][0]['SUM(seller_credit)']);
        if( $credit['SUM(seller_credit)'] < 0)$credit['SUM(seller_credit)']=0;			

		$query = array(
            'conditions' => array('integral.int_min <'=>($credit['SUM(seller_credit)']+1),'integral.int_max >'=>($credit['SUM(seller_credit)']-1)),
			'fields' => array('integral.*'), 
		);
        $credit_row = $integral = POP::exe('[@@:UM integral][@@:QU find:first]',$query);

	    if($integral){
            $integral = $integral['integral']['int_grade'];
        }else{
            $integral = 0;
        }		
        $seller_credit = $credit['SUM(seller_credit)'];		
		
        $this->set('category_list_new',$category_list_new);	
	    $this->set('header',$header);
	    $this->set('SHOP',$SHOP);
	    $this->set('shop_rank_arr',$shop_rank_arr);		
			
		$query = array(
            'conditions' => array('shoprequest.user_id' => $shop_id),
			'fields' => array('shoprequest.*'), 
		);
        $SHOPREQUEST = POP::exe('[@@:UM shoprequest][@@:QU find:first]',$query); 
		$SHOPREQUEST = $SHOPREQUEST['shoprequest'];			
	    $this->set('SHOPREQUEST',$SHOPREQUEST);	

		$query = array(
            'conditions' =>array('shopalbumcategory.shop_id' => $shop_id),
            'order' => 'sort_order asc',
			'fields' => array('shopalbumcategory.*'), 
		);
        $shop_album_category = POP::exe('[@@:UM shopalbumcategory][@@:QU find:all]',$query);
		$shop_album_cat = array();
		$shop_album_cat[0]= "临时图片夹";
		foreach($shop_album_category as $key => $value) {
			$shop_album_cat[$value['shopalbumcategory']['id']] =  $value['shopalbumcategory']['album_cat_name'];
		}

        $page =intval(Request::get_args('page'));       
		$url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		$tb = array('model' => 'imgsize',
					'fields' => array("imgsize.*"),
					'conditions' => array('imgsize.shopalbum_id' => $shopalbum_id,'imgsize.uid' => $shop_id),
					'order' => '',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
		$result =  $this->fpage($tb); 
		
	    $this->set('shop_album_cat',$shop_album_cat);
	    $this->set('result',$result);		
	    $this->set('user_id',$user_id);        
	    $this->set('shopalbum_id',$shopalbum_id);	
        if(empty($SHOP['shop_template'])){
		    $SHOP['shop_template'] = $this->isuservip ;
		}
        if(isset($privilege[11]) && $privilege[11]>0 ){
		    $SHOP['shop_template'] = $this->vipviews ;
		}		
		if($SHOP['shop_template']== $this->isuservip) {
		    $this->render('albumshow');	
		}else {
		    $this->render($SHOP['shop_template'].'/albumshow');		
		}		

	}	
	
	
	
	public function news($shop_id) {
        $shop_id = intval($shop_id);
        if(!$shop_id) {
	        exit("非法请求！");
        }
		if(isset($this->my->id)){$user_id = $this->my->id; }else{ $user_id = 0 ;}
		$query = array(
			'conditions' => array('shopinfo.id' => $shop_id), 
			'fields' => array('shopinfo.*'), 
		);
        $SHOP = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);		
        if(!$SHOP) { trigger_error("没有此商铺！");}//没有此商铺
		$SHOP = $SHOP['shopinfo'];		
        if($SHOP['lock_flg']) { trigger_error("商品不存在或商铺已锁定");}//锁定
        if($user_id!=$SHOP['user_id']){
	        if($SHOP['open_flg']) {
		        trigger_error("此商铺已关闭");
	        }//关闭
        }		
        if($user_id > 0){
            if($this->my->locked==1){
	            $this->C_session->destroy();
	            exit("您已经被锁定了");//非法操作
            }
        }
        //商铺分类
        $shop_rank = $SHOP['shop_categories'];			
		$shop_rank_arr = array();
        if($shop_rank) {
		    $cat = $shop_rank;
			$query = array(
				'fields' => array('shopcategories.*'), 
			);
	        $arr = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);	
            if($arr) {
			    $index = 0;
                $cats  = array();				
                while (1){
                    foreach ($arr AS $row){
                        if ($cat == $row['shopcategories']['id']){
                            $cat = $row['shopcategories']['parent_id'];
                            $cats[$index]['cat_id']   = $row['shopcategories']['id'];
                            $cats[$index]['cat_name'] = $row['shopcategories']['cat_name'];
                            $index++;
                            break;
                        }
                    }
                    if ($index == 0 || $cat == 0){
                        break;
                    }
                }				
                $shop_rank_arr = array_reverse($cats);				
			}			
		}		
        if ($shop_rank_arr){
	        $num = count($shop_rank_arr) - 1;
        }		

		$query = array(
			'conditions' => array('user.id' => $SHOP['user_id']), 
			'fields' => array('user.*'), 
		);
        $ranks = POP::exe('[@@:UM user][@@:QU find:first]',$query);        		
        $SHOP['rank_id'] = $ranks['user']['rank_id'];		
		$query = array(
			'conditions' => array('userrank.id' => $SHOP['rank_id']), 
			'fields' => array('userrank.*'), 
		);
        $user_rank = POP::exe('[@@:UM userrank][@@:QU find:first]',$query);                	
        $user_rank = $user_rank['userrank'];

        	
        $best_num = 8;
        $privilege = unserialize($user_rank['privilege']);
        if($privilege){
	        $best_num = $privilege[4];
        }
	    $header = array();
	    $header['title'] = "商铺新闻 - ".$SHOP['shop_name'];
	    $header['keywords'] = $SHOP['shop_management'];
	    $header['description'] = strip_tags($SHOP['shop_intro']);	

        $nav_selected="";

		$query = array(
            'conditions' => array('shopusercategorie.shop_id' => $shop_id),
            'order'=>'sort_order asc',
			'fields' => array('shopusercategorie.*'), 
		);
        $category_list = POP::exe('[@@:UM shopusercategorie][@@:QU find:all]',$query);


        $category_list_new = array();
        if(!empty($category_list)) {
	        foreach($category_list as $v) {
		        $category_list_new[$v['shopusercategorie']['id']]['id'] = $v['shopusercategorie']['id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_name'] = $v['shopusercategorie']['shop_cat_name'];
		        $category_list_new[$v['shopusercategorie']['id']]['parent_id'] = $v['shopusercategorie']['parent_id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_unit'] = $v['shopusercategorie']['shop_cat_unit'];
		        $category_list_new[$v['shopusercategorie']['id']]['sort_order'] = $v['shopusercategorie']['sort_order'];
	        }
        }		
        unset($category_list);
		
		$query = array(
				'joins' => array(
				array('type'=>'left','table'=>'userrank','conditions'=>array('user.rank_id' =>'userrank.id')),					
				),
				'fields' => array('userrank.rank_name'),
				'conditions' => array('user.id' =>$shop_id),
			);	
	    $rank_name = POP::exe('[@@:UM user][@@:QU find:first]',$query);

        //获取商家信用值
		$query = array(
            'conditions' => array('credit.seller' => $shop_id),
			'fields' => array('credit.seller_credit'),
			'calculate'=>'sum' 
		);
	    $credit = POP::exe('[@@:UM credit][@@:QU find:calculate]',$query);
        $credit['SUM(seller_credit)'] = intval($credit['sum.credit.seller_credit']);	    
        if( $credit['SUM(seller_credit)'] < 0)$credit['SUM(seller_credit)']=0;			

		$query = array(
            'conditions' => array('integral.int_min <'=>($credit['SUM(seller_credit)']+1),'integral.int_max >'=>($credit['SUM(seller_credit)']-1)),
			'fields' => array('integral.*'), 
		);
        $credit_row = $integral = POP::exe('[@@:UM integral][@@:QU find:first]',$query);
	    if($integral){
            $integral = $integral['integral']['int_grade'];
        }else{
            $integral = 0;
        }		
        $seller_credit = $credit['SUM(seller_credit)'];		
		
        $this->set('category_list_new',$category_list_new);	
	    $this->set('header',$header);
	    $this->set('SHOP',$SHOP);
	    $this->set('shop_rank_arr',$shop_rank_arr);		
	
		$query = array(
            'conditions' => array('shoprequest.user_id' => $shop_id),
			'fields' => array('shoprequest.*'), 
		);
        $SHOPREQUEST = POP::exe('[@@:UM shoprequest][@@:QU find:first]',$query); 
		$SHOPREQUEST = $SHOPREQUEST['shoprequest'];			
	    $this->set('SHOPREQUEST',$SHOPREQUEST);		
	
        $cat_id = intval(Request::get_args('id'));
        $page =intval(Request::get_args('page'));
		$conditions = array();
		$conditions['shoparticle.shop_id']= $shop_id;
		$conditions['shoparticle.is_show']= 1;	
        if($cat_id) {
		    $conditions['shoparticle.cat_id']= $cat_id;		    	        
        }        
		$url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		$tb = array('model' => 'shoparticle',
					'fields' => array("shoparticle.*"),
					'conditions' => $conditions,
					'order' => '',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
		$result =  $this->fpage($tb); 

		$query = array(
			'fields' => array('shoparticlecat.*'), 
		);
		$catlist = POP::exe('[@@:UM shoparticlecat][@@:QU find:all]',$query); 
		
		$catinfo = array();
        foreach($catlist as $key => $value) {
            $catinfo[$value['shoparticlecat']['id']] = $value['shoparticlecat'];
        }    		
        $this->set('catinfo',$catinfo);	
        $this->set('result',$result);
        $this->set('catlist',$catlist);	
        $this->set('cat_id',$cat_id);		
        if(empty($SHOP['shop_template'])){
		    $SHOP['shop_template'] = $this->isuservip ;
		}	
        if(isset($privilege[11]) && $privilege[11]>0 ){
		    $SHOP['shop_template'] = $this->vipviews ;
		}		
		if($SHOP['shop_template']== $this->isuservip) {
		    $this->render('news');	
		}else {
		    $this->render($SHOP['shop_template'].'/news');		
		}		
        
	}

	public function newshow($shop_id,$newsid) {
        $shop_id = intval($shop_id);
        $newsid = intval($newsid);		
        if(!$shop_id) {
	        exit("非法请求！");
        }
		if(isset($this->my->id)){$user_id = $this->my->id; }else{ $user_id = 0 ;}
		$query = array(
			'conditions' => array('shopinfo.id' => $shop_id), 
			'fields' => array('shopinfo.*'), 
		);
        $SHOP = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);		

        if(!$SHOP) { trigger_error("没有此商铺！");}//没有此商铺
		$SHOP = $SHOP['shopinfo'];		
        if($SHOP['lock_flg']) { trigger_error("商品不存在或商铺已锁定");}//锁定
        if($user_id!=$SHOP['user_id']){
	        if($SHOP['open_flg']) {
		        trigger_error("此商铺已关闭");
	        }//关闭
        }		
        if($user_id > 0){
            if($this->my->locked==1){
	            $this->C_session->destroy();
	            exit("您已经被锁定了");//非法操作
            }
        }
        //商铺分类
        $shop_rank = $SHOP['shop_categories'];			
		$shop_rank_arr = array();
        if($shop_rank) {
		    $cat = $shop_rank;
			$query = array(
				'fields' => array('shopcategories.*'), 
			);
	        $arr = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);
            if($arr) {
			    $index = 0;
                $cats  = array();				
                while (1){
                    foreach ($arr AS $row){
                        if ($cat == $row['shopcategories']['id']){
                            $cat = $row['shopcategories']['parent_id'];
                            $cats[$index]['cat_id']   = $row['shopcategories']['id'];
                            $cats[$index]['cat_name'] = $row['shopcategories']['cat_name'];
                            $index++;
                            break;
                        }
                    }
                    if ($index == 0 || $cat == 0){
                        break;
                    }
                }				
                $shop_rank_arr = array_reverse($cats);				
			}			
		}		
        if ($shop_rank_arr){
	        $num = count($shop_rank_arr) - 1;
        }	

        	
		$query = array(
			'conditions' => array('user.id' => $SHOP['user_id']), 
			'fields' => array('user.*'), 
		);
        $ranks = POP::exe('[@@:UM user][@@:QU find:first]',$query);        		
        $SHOP['rank_id'] = $ranks['user']['rank_id'];		
		$query = array(
			'conditions' => array('userrank.id' => $SHOP['rank_id']), 
			'fields' => array('userrank.*'), 
		);
        $user_rank = POP::exe('[@@:UM userrank][@@:QU find:first]',$query);                	
        $user_rank = $user_rank['userrank'];


        		
        $best_num = 8;
        $privilege = unserialize($user_rank['privilege']);
        if($privilege){
	        $best_num = $privilege[4];
        }
	    $header = array();
	    $header['title'] = "商铺新闻 - ".$SHOP['shop_name'];
	    $header['keywords'] = $SHOP['shop_management'];
	    $header['description'] = strip_tags($SHOP['shop_intro']);	

        $nav_selected="";
		$query = array(
            'conditions' => array('shopusercategorie.shop_id' => $shop_id),
            'order'=>'sort_order asc',
			'fields' => array('shopusercategorie.*'), 
		);
        $category_list = POP::exe('[@@:UM shopusercategorie][@@:QU find:all]',$query);

        $category_list_new = array();
        if(!empty($category_list)) {
	        foreach($category_list as $v) {
		        $category_list_new[$v['shopusercategorie']['id']]['id'] = $v['shopusercategorie']['id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_name'] = $v['shopusercategorie']['shop_cat_name'];
		        $category_list_new[$v['shopusercategorie']['id']]['parent_id'] = $v['shopusercategorie']['parent_id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_unit'] = $v['shopusercategorie']['shop_cat_unit'];
		        $category_list_new[$v['shopusercategorie']['id']]['sort_order'] = $v['shopusercategorie']['sort_order'];
	        }
        }		
        unset($category_list);
		
		$query = array(
				'joins' => array(
				array('type'=>'left','table'=>'userrank','conditions'=>array('user.rank_id' =>'userrank.id')),					
				),
				'fields' => array('userrank.rank_name'),
				'conditions' => array('user.id' =>$shop_id),
			);	
	    $rank_name = POP::exe('[@@:UM user][@@:QU find:first]',$query);

        //获取商家信用值
		$query = array(
            'conditions' => array('credit.seller' => $shop_id),
			'fields' => array('credit.seller_credit'),
			'calculate'=>'sum' 
		);
	    $credit = POP::exe('[@@:UM credit][@@:QU find:calculate]',$query);
	    
        $credit['SUM(seller_credit)'] = intval($credit['sum.credit.seller_credit']);
        if( $credit['SUM(seller_credit)'] < 0)$credit['SUM(seller_credit)']=0;			

		$query = array(
            'conditions' => array('integral.int_min <'=>($credit['SUM(seller_credit)']+1),'integral.int_max >'=>($credit['SUM(seller_credit)']-1)),
			'fields' => array('integral.*'), 
		);
        $credit_row = $integral = POP::exe('[@@:UM integral][@@:QU find:first]',$query);

	    if($integral){
            $integral = $integral['integral']['int_grade'];
        }else{
            $integral = 0;
        }		
        $seller_credit = $credit['SUM(seller_credit)'];		

		$query = array(
            'conditions' => array('shoprequest.user_id' => $shop_id),
			'fields' => array('shoprequest.*'), 
		);
        $SHOPREQUEST = POP::exe('[@@:UM shoprequest][@@:QU find:first]',$query); 
		$SHOPREQUEST = $SHOPREQUEST['shoprequest'];			
	    $this->set('SHOPREQUEST',$SHOPREQUEST);	
		
        $this->set('category_list_new',$category_list_new);	
	    $this->set('header',$header);
	    $this->set('SHOP',$SHOP);
	    $this->set('shop_rank_arr',$shop_rank_arr);		
	
		$query = array(
            'conditions' => array('shoparticle.id' => $newsid,'shoparticle.shop_id' => $shop_id),
			'fields' => array('shoparticle.*'), 
		);
		$news_info = POP::exe('[@@:UM shoparticle][@@:QU find:first]',$query);
		$news_info = $news_info['shoparticle'];	

		$query = array(
			'fields' => array('shoparticlecat.*'), 
		);
		$catlist = POP::exe('[@@:UM shoparticlecat][@@:QU find:all]',$query); 
       
		$catinfo = array();
        foreach($catlist as $key => $value) {
            $catinfo[$value['shoparticlecat']['id']] = $value['shoparticlecat'];
        } 
 		
        $this->set('catinfo',$catinfo);	
        $this->set('catlist',$catlist);	
        $this->set('news_info',$news_info);				
        if(empty($SHOP['shop_template'])){
		    $SHOP['shop_template'] = $this->isuservip ;
		}
        if(isset($privilege[11]) && $privilege[11]>0 ){
		    $SHOP['shop_template'] = $this->vipviews ;
		}		
		if($SHOP['shop_template']== $this->isuservip) {
		    $this->render('newshow');	
		}else {
		    $this->render($SHOP['shop_template'].'/newshow');		
		}	
		
        
	}	

	
	public function shopintro($shop_id) {
        $shop_id = intval($shop_id);	
        if(!$shop_id) {
	        exit("非法请求！");
        }
		if(isset($this->my->id)){$user_id = $this->my->id; }else{ $user_id = 0 ;}
		$query = array(
			'conditions' => array('shopinfo.id' => $shop_id), 
			'fields' => array('shopinfo.*'), 
		);
        $SHOP = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);		
        if(!$SHOP) { trigger_error("没有此商铺！");}//没有此商铺
		$SHOP = $SHOP['shopinfo'];		
        if($SHOP['lock_flg']) { trigger_error("商品不存在或商铺已锁定");}//锁定
        if($user_id!=$SHOP['user_id']){
	        if($SHOP['open_flg']) {
		        trigger_error("此商铺已关闭");
	        }//关闭
        }		
        if($user_id > 0){
            if($this->my->locked==1){
	            $this->C_session->destroy();
	            exit("您已经被锁定了");//非法操作
            }
        }
        //商铺分类
        $shop_rank = $SHOP['shop_categories'];			
		$shop_rank_arr = array();
        if($shop_rank) {
		    $cat = $shop_rank;
			$query = array(
				'fields' => array('shopcategories.*'), 
			);
	        $arr = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);
            if($arr) {
			    $index = 0;
                $cats  = array();				
                while (1){
                    foreach ($arr AS $row){
                        if ($cat == $row['shopcategories']['id']){
                            $cat = $row['shopcategories']['parent_id'];
                            $cats[$index]['cat_id']   = $row['shopcategories']['id'];
                            $cats[$index]['cat_name'] = $row['shopcategories']['cat_name'];
                            $index++;
                            break;
                        }
                    }
                    if ($index == 0 || $cat == 0){
                        break;
                    }
                }				
                $shop_rank_arr = array_reverse($cats);				
			}			
		}		
        if ($shop_rank_arr){
	        $num = count($shop_rank_arr) - 1;
        }		

		$query = array(
			'conditions' => array('user.id' => $SHOP['user_id']), 
			'fields' => array('user.*'), 
		);
        $ranks = POP::exe('[@@:UM user][@@:QU find:first]',$query);        		
        $SHOP['rank_id'] = $ranks['user']['rank_id'];		
		$query = array(
			'conditions' => array('userrank.id' => $SHOP['rank_id']), 
			'fields' => array('userrank.*'), 
		);
        $user_rank = POP::exe('[@@:UM userrank][@@:QU find:first]',$query);                	
        $user_rank = $user_rank['userrank'];

		
        $best_num = 8;
        $privilege = unserialize($user_rank['privilege']);
        if($privilege){
	        $best_num = $privilege[4];
        }
	    $header = array();
	    $header['title'] = "商铺联系我们 - ".$SHOP['shop_name'];
	    $header['keywords'] = $SHOP['shop_management'];
	    $header['description'] = strip_tags($SHOP['shop_intro']);	

        $nav_selected="";
		$query = array(
            'conditions' => array('shopusercategorie.shop_id' => $shop_id),
            'order'=>'sort_order asc',
			'fields' => array('shopusercategorie.*'), 
		);
        $category_list = POP::exe('[@@:UM shopusercategorie][@@:QU find:all]',$query);

        $category_list_new = array();
        if(!empty($category_list)) {
	        foreach($category_list as $v) {
		        $category_list_new[$v['shopusercategorie']['id']]['id'] = $v['shopusercategorie']['id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_name'] = $v['shopusercategorie']['shop_cat_name'];
		        $category_list_new[$v['shopusercategorie']['id']]['parent_id'] = $v['shopusercategorie']['parent_id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_unit'] = $v['shopusercategorie']['shop_cat_unit'];
		        $category_list_new[$v['shopusercategorie']['id']]['sort_order'] = $v['shopusercategorie']['sort_order'];
	        }
        }		
        unset($category_list);
		
		$query = array(
				'joins' => array(
				array('type'=>'left','table'=>'userrank','conditions'=>array('user.rank_id' =>'userrank.id')),					
				),
				'fields' => array('userrank.rank_name'),
				'conditions' => array('user.id' =>$shop_id),
			);	
	    $rank_name = POP::exe('[@@:UM user][@@:QU find:first]',$query);

        //获取商家信用值
		$query = array(
            'conditions' => array('credit.seller' => $shop_id),
			'fields' => array('credit.seller_credit'),
			'calculate'=>'sum' 
		);
	    $credit = POP::exe('[@@:UM credit][@@:QU find:calculate]',$query);
	    
        $credit['SUM(seller_credit)'] = intval($credit[0][0]['SUM(seller_credit)']);
        if( $credit['SUM(seller_credit)'] < 0)$credit['SUM(seller_credit)']=0;			

		$query = array(
            'conditions' => array('integral.int_min <'=>($credit['SUM(seller_credit)']+1),'integral.int_max >'=>($credit['SUM(seller_credit)']-1)),
			'fields' => array('integral.*'), 
		);
        $credit_row = $integral = POP::exe('[@@:UM integral][@@:QU find:first]',$query);

	    if($integral){
            $integral = $integral['integral']['int_grade'];
        }else{
            $integral = 0;
        }		
        $seller_credit = $credit['SUM(seller_credit)'];		

		$query = array(
            'conditions' => array('shoprequest.user_id' => $shop_id),
			'fields' => array('shoprequest.*'), 
		);
        $SHOPREQUEST = POP::exe('[@@:UM shoprequest][@@:QU find:first]',$query); 
		$SHOPREQUEST = $SHOPREQUEST['shoprequest'];			
	    $this->set('SHOPREQUEST',$SHOPREQUEST);			
		
        $this->set('category_list_new',$category_list_new);	
	    $this->set('header',$header);
	    $this->set('SHOP',$SHOP);
	    $this->set('shop_rank_arr',$shop_rank_arr);		
		
        if(empty($SHOP['shop_template'])){
		    $SHOP['shop_template'] = $this->isuservip ;
		}
        if(isset($privilege[11]) && $privilege[11]>0 ){
		    $SHOP['shop_template'] = $this->vipviews ;
		}		
		if($SHOP['shop_template']== $this->isuservip) {
		    $this->render('shopintro');	
		}else {
		    $this->render($SHOP['shop_template'].'/shopintro');		
		}     
	}	
	
	
	public function contactus($shop_id) {
        $shop_id = intval($shop_id);	
        if(!$shop_id) {
	        exit("非法请求！");
        }
		if(isset($this->my->id)){$user_id = $this->my->id; }else{ $user_id = 0 ;}
		$query = array(
			'conditions' => array('shopinfo.id' => $shop_id), 
			'fields' => array('shopinfo.*'), 
		);
        $SHOP = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);		
        if(!$SHOP) { trigger_error("没有此商铺！");}//没有此商铺
		$SHOP = $SHOP['shopinfo'];		
        if($SHOP['lock_flg']) { trigger_error("商品不存在或商铺已锁定");}//锁定
        if($user_id!=$SHOP['user_id']){
	        if($SHOP['open_flg']) {
		        trigger_error("此商铺已关闭");
	        }//关闭
        }		
        if($user_id > 0){
            if($this->my->locked==1){
	            $this->C_session->destroy();
	            exit("您已经被锁定了");//非法操作
            }
        }
        //商铺分类
        $shop_rank = $SHOP['shop_categories'];			
		$shop_rank_arr = array();
        if($shop_rank) {
		    $cat = $shop_rank;
			$query = array(
				'fields' => array('shopcategories.*'), 
			);
	        $arr = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);	
            if($arr) {
			    $index = 0;
                $cats  = array();				
                while (1){
                    foreach ($arr AS $row){
                        if ($cat == $row['shopcategories']['id']){
                            $cat = $row['shopcategories']['parent_id'];
                            $cats[$index]['cat_id']   = $row['shopcategories']['id'];
                            $cats[$index]['cat_name'] = $row['shopcategories']['cat_name'];
                            $index++;
                            break;
                        }
                    }
                    if ($index == 0 || $cat == 0){
                        break;
                    }
                }				
                $shop_rank_arr = array_reverse($cats);				
			}			
		}		
        if ($shop_rank_arr){
	        $num = count($shop_rank_arr) - 1;
        }		

		$query = array(
			'conditions' => array('user.id' => $SHOP['user_id']), 
			'fields' => array('user.*'), 
		);
        $ranks = POP::exe('[@@:UM user][@@:QU find:first]',$query);        		
        $SHOP['rank_id'] = $ranks['user']['rank_id'];		
		$query = array(
			'conditions' => array('userrank.id' => $SHOP['rank_id']), 
			'fields' => array('userrank.*'), 
		);
        $user_rank = POP::exe('[@@:UM userrank][@@:QU find:first]',$query);                	
        $user_rank = $user_rank['userrank'];

        		
        $best_num = 8;
        $privilege = unserialize($user_rank['privilege']);
        if($privilege){
	        $best_num = $privilege[4];
        }
	    $header = array();
	    $header['title'] = "商铺联系我们 - ".$SHOP['shop_name'];
	    $header['keywords'] = $SHOP['shop_management'];
	    $header['description'] = strip_tags($SHOP['shop_intro']);	

        $nav_selected="";
		$query = array(
            'conditions' => array('shopusercategorie.shop_id' => $shop_id),
            'order'=>'sort_order asc',
			'fields' => array('shopusercategorie.*'), 
		);
        $category_list = POP::exe('[@@:UM shopusercategorie][@@:QU find:all]',$query);

        $category_list_new = array();
        if(!empty($category_list)) {
	        foreach($category_list as $v) {
		        $category_list_new[$v['shopusercategorie']['id']]['id'] = $v['shopusercategorie']['id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_name'] = $v['shopusercategorie']['shop_cat_name'];
		        $category_list_new[$v['shopusercategorie']['id']]['parent_id'] = $v['shopusercategorie']['parent_id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_unit'] = $v['shopusercategorie']['shop_cat_unit'];
		        $category_list_new[$v['shopusercategorie']['id']]['sort_order'] = $v['shopusercategorie']['sort_order'];
	        }
        }		
        unset($category_list);
		
		$query = array(
				'joins' => array(
				array('type'=>'left','table'=>'userrank','conditions'=>array('user.rank_id' =>'userrank.id')),					
				),
				'fields' => array('userrank.rank_name'),
				'conditions' => array('user.id' =>$shop_id),
			);	
	    $rank_name = POP::exe('[@@:UM user][@@:QU find:first]',$query);

        //获取商家信用值
		$query = array(
            'conditions' => array('credit.seller' => $shop_id),
			'fields' => array('credit.seller_credit'),
			'calculate'=>'sum' 
		);
	    $credit = POP::exe('[@@:UM credit][@@:QU find:calculate]',$query);
	    
        $credit['SUM(seller_credit)'] = intval($credit['sum.credit.seller_credit']);
        if( $credit['SUM(seller_credit)'] < 0)$credit['SUM(seller_credit)']=0;			

		$query = array(
            'conditions' => array('integral.int_min <'=>($credit['SUM(seller_credit)']+1),'integral.int_max >'=>($credit['SUM(seller_credit)']-1)),
			'fields' => array('integral.*'), 
		);
        $credit_row = $integral = POP::exe('[@@:UM integral][@@:QU find:first]',$query);

	    if($integral){
            $integral = $integral['integral']['int_grade'];
        }else{
            $integral = 0;
        }		
        $seller_credit = $credit['SUM(seller_credit)'];		

		$query = array(
            'conditions' => array('shoprequest.user_id' => $shop_id),
			'fields' => array('shoprequest.*'), 
		);
        $SHOPREQUEST = POP::exe('[@@:UM shoprequest][@@:QU find:first]',$query); 
		$SHOPREQUEST = $SHOPREQUEST['shoprequest'];			
	    $this->set('SHOPREQUEST',$SHOPREQUEST);			
		
        $this->set('category_list_new',$category_list_new);	
	    $this->set('header',$header);
	    $this->set('SHOP',$SHOP);
	    $this->set('shop_rank_arr',$shop_rank_arr);		
		
        if(empty($SHOP['shop_template'])){
		    $SHOP['shop_template'] = $this->isuservip ;
		}
        if(isset($privilege[11]) && $privilege[11]>0 ){
		    $SHOP['shop_template'] = $this->vipviews ;
		}		
		if($SHOP['shop_template']== $this->isuservip) {
		    $this->render('contactus');	
		}else {
		    $this->render($SHOP['shop_template'].'/contactus');		
		}     
	}
	
	public function join($shop_id) {
	
        $shop_id = intval($shop_id);	
        if(!$shop_id) {
	        exit("非法请求！");
        }
		if(isset($this->my->id)){$user_id = $this->my->id; }else{ $user_id = 0 ;}
		$query = array(
			'conditions' => array('shopinfo.id' => $shop_id), 
			'fields' => array('shopinfo.*'), 
		);
        $SHOP = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);		
        if(!$SHOP) { trigger_error("没有此商铺！");}//没有此商铺
		$SHOP = $SHOP['shopinfo'];		
        if($SHOP['lock_flg']) { trigger_error("商品不存在或商铺已锁定");}//锁定
        if($user_id!=$SHOP['user_id']){
	        if($SHOP['open_flg']) {
		        trigger_error("此商铺已关闭");
	        }//关闭
        }		
        if($user_id > 0){
            if($this->my->locked==1){
	            $this->C_session->destroy();
	            exit("您已经被锁定了");//非法操作
            }
        }
        //商铺分类
        $shop_rank = $SHOP['shop_categories'];			
		$shop_rank_arr = array();
        if($shop_rank) {
		    $cat = $shop_rank;
			$query = array(
				'fields' => array('shopcategories.*'), 
			);
	        $arr = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);
            if($arr) {
			    $index = 0;
                $cats  = array();				
                while (1){
                    foreach ($arr AS $row){
                        if ($cat == $row['shopcategories']['id']){
                            $cat = $row['shopcategories']['parent_id'];
                            $cats[$index]['cat_id']   = $row['shopcategories']['id'];
                            $cats[$index]['cat_name'] = $row['shopcategories']['cat_name'];
                            $index++;
                            break;
                        }
                    }
                    if ($index == 0 || $cat == 0){
                        break;
                    }
                }				
                $shop_rank_arr = array_reverse($cats);				
			}			
		}		
        if ($shop_rank_arr){
	        $num = count($shop_rank_arr) - 1;
        }		

		$query = array(
			'conditions' => array('user.id' => $SHOP['user_id']), 
			'fields' => array('user.*'), 
		);
        $ranks = POP::exe('[@@:UM user][@@:QU find:first]',$query);        		
        $SHOP['rank_id'] = $ranks['user']['rank_id'];		
		$query = array(
			'conditions' => array('userrank.id' => $SHOP['rank_id']), 
			'fields' => array('userrank.*'), 
		);
        $user_rank = POP::exe('[@@:UM userrank][@@:QU find:first]',$query);                	
        $user_rank = $user_rank['userrank'];

        		
        $best_num = 8;
        $privilege = unserialize($user_rank['privilege']);
        if($privilege){
	        $best_num = $privilege[4];
        }
	    $header = array();
	    $header['title'] = "商铺联系我们 - ".$SHOP['shop_name'];
	    $header['keywords'] = $SHOP['shop_management'];
	    $header['description'] = strip_tags($SHOP['shop_intro']);	

        $nav_selected="";
		$query = array(
            'conditions' => array('shopusercategorie.shop_id' => $shop_id),
            'order'=>'sort_order asc',
			'fields' => array('shopusercategorie.*'), 
		);
        $category_list = POP::exe('[@@:UM shopusercategorie][@@:QU find:all]',$query);


        $category_list_new = array();
        if(!empty($category_list)) {
	        foreach($category_list as $v) {
		        $category_list_new[$v['shopusercategorie']['id']]['id'] = $v['shopusercategorie']['id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_name'] = $v['shopusercategorie']['shop_cat_name'];
		        $category_list_new[$v['shopusercategorie']['id']]['parent_id'] = $v['shopusercategorie']['parent_id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_unit'] = $v['shopusercategorie']['shop_cat_unit'];
		        $category_list_new[$v['shopusercategorie']['id']]['sort_order'] = $v['shopusercategorie']['sort_order'];
	        }
        }		
        unset($category_list);
		
		$query = array(
				'joins' => array(
				array('type'=>'left','table'=>'userrank','conditions'=>array('user.rank_id' =>'userrank.id')),					
				),
				'fields' => array('userrank.rank_name'),
				'conditions' => array('user.id' =>$shop_id),
			);	
	    $rank_name = POP::exe('[@@:UM user][@@:QU find:first]',$query);

        //获取商家信用值
		$query = array(
            'conditions' => array('credit.seller' => $shop_id),
			'fields' => array('credit.seller_credit'),
			'calculate'=>'sum' 
		);
	    $credit = POP::exe('[@@:UM credit][@@:QU find:calculate]',$query);
	    
        $credit['SUM(seller_credit)'] = intval($credit['sum.credit.seller_credit']);
        if( $credit['SUM(seller_credit)'] < 0)$credit['SUM(seller_credit)']=0;			

		$query = array(
            'conditions' => array('integral.int_min <'=>($credit['SUM(seller_credit)']+1),'integral.int_max >'=>($credit['SUM(seller_credit)']-1)),
			'fields' => array('integral.*'), 
		);
        $credit_row = $integral = POP::exe('[@@:UM integral][@@:QU find:first]',$query);

	    if($integral){
            $integral = $integral['integral']['int_grade'];
        }else{
            $integral = 0;
        }		
        $seller_credit = $credit['SUM(seller_credit)'];		

		$query = array(
            'conditions' => array('shoprequest.user_id' => $shop_id),
			'fields' => array('shoprequest.*'), 
		);
        $SHOPREQUEST = POP::exe('[@@:UM shoprequest][@@:QU find:first]',$query); 
		$SHOPREQUEST = $SHOPREQUEST['shoprequest'];			
	    $this->set('SHOPREQUEST',$SHOPREQUEST);			
		
        $this->set('category_list_new',$category_list_new);	
	    $this->set('header',$header);
	    $this->set('SHOP',$SHOP);
	    $this->set('shop_rank_arr',$shop_rank_arr);		
			
	
        if(empty($SHOP['shop_template'])){
		    $SHOP['shop_template'] = $this->isuservip ;
		}	
        if(isset($privilege[11]) && $privilege[11]>0 ){
		    $SHOP['shop_template'] = $this->vipviews ;
		}		
		if($SHOP['shop_template']== $this->isuservip) {
		    $this->render('join');	
		}else {
		    $this->render($SHOP['shop_template'].'/join');		
		}		
	
	}

	public function credit($shop_id) {
        $shop_id = intval($shop_id);	
        if(!$shop_id) {
	        exit("非法请求！");
        }
		if(isset($this->my->id)){$user_id = $this->my->id; }else{ $user_id = 0 ;}
		$query = array(
			'conditions' => array('shopinfo.id' => $shop_id), 
			'fields' => array('shopinfo.*'), 
		);
        $SHOP = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);		
        if(!$SHOP) { trigger_error("没有此商铺！");}//没有此商铺
		$SHOP = $SHOP['shopinfo'];		
        if($SHOP['lock_flg']) { trigger_error("商品不存在或商铺已锁定");}//锁定
        if($user_id!=$SHOP['user_id']){
	        if($SHOP['open_flg']) {
		        trigger_error("此商铺已关闭");
	        }//关闭
        }		
        if($user_id > 0){
            if($this->my->locked==1){
	            $this->C_session->destroy();
	            exit("您已经被锁定了");//非法操作
            }
        }
        //商铺分类
        $shop_rank = $SHOP['shop_categories'];			
		$shop_rank_arr = array();
        if($shop_rank) {
		    $cat = $shop_rank;
			$query = array(
				'fields' => array('shopcategories.*'), 
			);
	        $arr = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);
            if($arr) {
			    $index = 0;
                $cats  = array();				
                while (1){
                    foreach ($arr AS $row){
                        if ($cat == $row['shopcategories']['id']){
                            $cat = $row['shopcategories']['parent_id'];
                            $cats[$index]['cat_id']   = $row['shopcategories']['id'];
                            $cats[$index]['cat_name'] = $row['shopcategories']['cat_name'];
                            $index++;
                            break;
                        }
                    }
                    if ($index == 0 || $cat == 0){
                        break;
                    }
                }				
                $shop_rank_arr = array_reverse($cats);				
			}			
		}		
        if ($shop_rank_arr){
	        $num = count($shop_rank_arr) - 1;
        }		
		$query = array(
			'conditions' => array('user.id' => $SHOP['user_id']), 
			'fields' => array('user.*'), 
		);
        $ranks = POP::exe('[@@:UM user][@@:QU find:first]',$query);        		
        $SHOP['rank_id'] = $ranks['user']['rank_id'];		
		$query = array(
			'conditions' => array('userrank.id' => $SHOP['rank_id']), 
			'fields' => array('userrank.*'), 
		);
        $user_rank = POP::exe('[@@:UM userrank][@@:QU find:first]',$query);                	
        $user_rank = $user_rank['userrank'];
        	
        $best_num = 8;
        $privilege = unserialize($user_rank['privilege']);
        if($privilege){
	        $best_num = $privilege[4];
        }
	    $header = array();
	    $header['title'] = "商铺联系我们 - ".$SHOP['shop_name'];
	    $header['keywords'] = $SHOP['shop_management'];
	    $header['description'] = strip_tags($SHOP['shop_intro']);	

        $nav_selected="";
		$query = array(
            'conditions' => array('shopusercategorie.shop_id' => $shop_id),
            'order'=>'sort_order asc',
			'fields' => array('shopusercategorie.*'), 
		);
        $category_list = POP::exe('[@@:UM shopusercategorie][@@:QU find:all]',$query);


        $category_list_new = array();
        if(!empty($category_list)) {
	        foreach($category_list as $v) {
		        $category_list_new[$v['shopusercategorie']['id']]['id'] = $v['shopusercategorie']['id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_name'] = $v['shopusercategorie']['shop_cat_name'];
		        $category_list_new[$v['shopusercategorie']['id']]['parent_id'] = $v['shopusercategorie']['parent_id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_unit'] = $v['shopusercategorie']['shop_cat_unit'];
		        $category_list_new[$v['shopusercategorie']['id']]['sort_order'] = $v['shopusercategorie']['sort_order'];
	        }
        }		
        unset($category_list);
		
		$query = array(
				'joins' => array(
				array('type'=>'left','table'=>'userrank','conditions'=>array('user.rank_id' =>'userrank.id')),					
				),
				'fields' => array('userrank.rank_name'),
				'conditions' => array('user.id' =>$shop_id),
			);	
	    $rank_name = POP::exe('[@@:UM user][@@:QU find:first]',$query);

        //获取商家信用值
		$query = array(
            'conditions' => array('credit.seller' => $shop_id),
			'fields' => array('credit.seller_credit'),
			'calculate'=>'sum' 
		);
	    $credit = POP::exe('[@@:UM credit][@@:QU find:calculate]',$query);
	    
        $credit['SUM(seller_credit)'] = intval($credit['sum.credit.seller_credit']);
        if( $credit['SUM(seller_credit)'] < 0)$credit['SUM(seller_credit)']=0;			

		$query = array(
            'conditions' => array('integral.int_min <'=>($credit['SUM(seller_credit)']+1),'integral.int_max >'=>($credit['SUM(seller_credit)']-1)),
			'fields' => array('integral.*'), 
		);
        $credit_row = $integral = POP::exe('[@@:UM integral][@@:QU find:first]',$query);

	    if($integral){
            $integral = $integral['integral']['int_grade'];
        }else{
            $integral = 0;
        }		
        $seller_credit = $credit['SUM(seller_credit)'];		
		
        $this->set('category_list_new',$category_list_new);	
	    $this->set('header',$header);
	    $this->set('SHOP',$SHOP);
	    $this->set('shop_rank_arr',$shop_rank_arr);		
	
		$query = array(
            'conditions' => array('shoprequest.user_id' => $shop_id),
			'fields' => array('shoprequest.*'), 
		);
        $SHOPREQUEST = POP::exe('[@@:UM shoprequest][@@:QU find:first]',$query); 
		$SHOPREQUEST = $SHOPREQUEST['shoprequest'];			
	    $this->set('SHOPREQUEST',$SHOPREQUEST);		
	
        /* 本页面信息处理 */
        $user_id=$SHOP['user_id'];
		$query = array(
            'conditions' => array('or' => array('buyer'=>$user_id,'seller'=>$user_id)),
			'fields' => array('credit.*'), 
		);
        $credit = POP::exe('[@@:UM credit][@@:QU find:all]',$query); 

        $seller_credit=$seller_credit_num = $seller_credit_before=$seller_credit_sex=0;
        $seller_credit_num_2 = $seller_credit_before_2=$seller_credit_sex_2=0;
        $seller_credit_num_1 = $seller_credit_before_1=$seller_credit_sex_1=0;
        $seller_credit_num_0 = $seller_credit_before_0=$seller_credit_sex_0=0;
        $seller_credit_month=$seller_credit_month_0=$seller_credit_month_1=$seller_credit_month_2=0;
        $seller_credit_weed=$seller_credit_weed_0=$seller_credit_weed_1=$seller_credit_weed_2=0;

        $buyer_credit=$buyer_credit_num = $buyer_credit_before=$buyer_credit_sex=0;
        $buyer_credit_num_2 = $buyer_credit_before_2=$buyer_credit_sex_2=0;
        $buyer_credit_num_1 = $buyer_credit_before_1=$buyer_credit_sex_1=0;
        $buyer_credit_num_0 = $buyer_credit_before_0=$buyer_credit_sex_0=0;
        $buyer_credit_month=$buyer_credit_month_0=$buyer_credit_month_1=$buyer_credit_month_2=0;
        $buyer_credit_weed=$buyer_credit_weed_0=$buyer_credit_weed_1=$buyer_credit_weed_2=0;
		Loader::import('lib.ctime');
		$ctime = new ctime();
        $SexMonth = $ctime->time_stamp() - (180 * 24 * 60 * 60);
        $SexMonth = date('Y-m-d', $SexMonth);
        $LastMonth = $ctime->time_stamp() - (30 * 24 * 60 * 60);
        $LastMonth = date('Y-m-d', $LastMonth);
        $LastWeek = $ctime->time_stamp() - (7 * 24 * 60 * 60);
        $LastWeek = date('Y-m-d', $LastWeek);
        foreach ($credit as $key=>$val){
		$val = $val['credit'];
	    //作为卖家
	    if($val['seller']==$user_id){
		    if($val['seller_evaltime']<$SexMonth){
			    if($val['seller_credit']==1){
				    $seller_credit_before_2++;
			    }
			    if($val['seller_credit']==0){
				    $seller_credit_before_1++;
			    }
			    if($val['seller_credit']==-1){
				    $seller_credit_before_0++;
			    }
			    $seller_credit_before=$seller_credit_before_0+$seller_credit_before_1+$seller_credit_before_2;
		    }else {
		        if($val['seller_evaltime']>=$SexMonth){
			        if($val['seller_credit']==1){
				        $seller_credit_sex_2++;
			        }
			        if($val['seller_credit']==0){
				        $seller_credit_sex_1++;
			        }
			        if($val['seller_credit']==-1){
				        $seller_credit_sex_0++;
			        }
			        $seller_credit_sex=$seller_credit_sex_0+$seller_credit_sex_1+$seller_credit_sex_2;

			        if($val['seller_evaltime']>=$LastMonth){
				        if($val['seller_credit']==1){
					        $seller_credit_month_2++;
				        }
				        if($val['seller_credit']==0){
					        $seller_credit_month_1++;
				        }
				        if($val['seller_credit']==-1){
					        $seller_credit_month_0++;
				        }
				        $seller_credit_month=$seller_credit_month_0+$seller_credit_month_1+$seller_credit_month_2;

				        if($val['seller_evaltime']>=$LastWeek){
					        if($val['seller_credit']==1){
						        $seller_credit_weed_2++;
					        }
					        if($val['seller_credit']==0){
						        $seller_credit_weed_1++;
					        }
					        if($val['seller_credit']==-1){
						        $seller_credit_weed_0++;
					        }
					        $seller_credit_weed=$seller_credit_weed_0+$seller_credit_weed_1+$seller_credit_weed_2;
				        }
			        }
		        }
			}
		    $seller_credit=$seller_credit+$val['seller_credit'];
	    }
	    //作为买家
	    if($val['buyer']==$user_id){
		    if($val['buyer_evaltime']<$SexMonth){
			    if($val['buyer_credit']==1){
				    $buyer_credit_before_2++;
			    }
			    if($val['buyer_credit']==0){
				    $buyer_credit_before_1++;
			    }
			    if($val['buyer_credit']==-1){
				    $buyer_credit_before_0++;
			    }
			    $buyer_credit_before=$buyer_credit_before_0+$buyer_credit_before_1+$buyer_credit_before_2;
		    }else {
		        if($val['buyer_evaltime']>=$SexMonth){
			        if($val['buyer_credit']==1){
				        $buyer_credit_sex_2++;
			        }
			        if($val['buyer_credit']==0){
				        $buyer_credit_sex_1++;
			        }
			        if($val['buyer_credit']==-1){
				        $buyer_credit_sex_0++;
			        }
			        $buyer_credit_sex=$buyer_credit_sex_0+$buyer_credit_sex_1+$buyer_credit_sex_2;

			        if($val['buyer_evaltime']>=$LastMonth){
				        if($val['buyer_credit']==1){
					        $buyer_credit_month_2++;
				        }
				        if($val['buyer_credit']==0){
					        $buyer_credit_month_1++;
				        }
				        if($val['buyer_credit']==-1){
					        $buyer_credit_month_0++;
				        }
				        $buyer_credit_month=$buyer_credit_month_0+$buyer_credit_month_1+$buyer_credit_month_2;

				        if($val['buyer_evaltime']>=$LastWeek){
					        if($val['buyer_credit']==1){
						        $buyer_credit_weed_2++;
					        }
					        if($val['buyer_credit']==0){
						        $buyer_credit_weed_1++;
					        }
					        if($val['buyer_credit']==-1){
						        $buyer_credit_weed_0++;
					        }
					    $buyer_credit_weed=$buyer_credit_weed_0+$buyer_credit_weed_1+$buyer_credit_weed_2;
				        }
			        }
		        }
			}
		    $buyer_credit=$buyer_credit+$val['buyer_credit'];
	    }
        }
        if( $buyer_credit < 0)$buyer_credit = 0;
        if( $seller_credit < 0)$seller_credit = 0;

		$query = array(
            'conditions' => array('integral.int_min <'=>($seller_credit+1),'integral.int_max >'=>($seller_credit-1)),
			'fields' => array('integral.*'), 
		);
		$tempintegral = POP::exe('[@@:UM integral][@@:QU find:first]',$query);

	    if($tempintegral){
            $seller_integral = $tempintegral['integral']['int_grade'];
        }else{
            $seller_integral = 0;
        }		
        $seller_credit_num = $seller_credit_before+$seller_credit_sex;
        $seller_credit_num_2 = $seller_credit_before_2+$seller_credit_sex_2;
        $seller_credit_num_1 = $seller_credit_before_1+$seller_credit_sex_1;
        $seller_credit_num_0 = $seller_credit_before_0+$seller_credit_sex_0;
        $seller_percentage=0;
        if($seller_credit_num!=0){
	        $seller_percentage = sub_str($seller_credit_num_2/$seller_credit_num,6)*100;
        }

		$query = array(
            'conditions' => array('integral.int_min <'=>($buyer_credit+1),'integral.int_max >'=>($buyer_credit-1)),
			'fields' => array('integral.*'), 
		);
		$tempintegral = POP::exe('[@@:UM integral][@@:QU find:first]',$query);

	    if($tempintegral){
            $buyer_integral = $tempintegral['integral']['int_grade'];
        }else{
            $buyer_integral = 0;
        }		
        $buyer_credit_num = $buyer_credit_before+$buyer_credit_sex;
        $buyer_credit_num_2 = $buyer_credit_before_2+$buyer_credit_sex_2;
        $buyer_credit_num_1 = $buyer_credit_before_1+$buyer_credit_sex_1;
        $buyer_credit_num_0 = $buyer_credit_before_0+$buyer_credit_sex_0;
        $buyer_percentage=0;
        if($buyer_credit_num!=0){
	        $buyer_percentage = sub_str($buyer_credit_num_2/$buyer_credit_num,6)*100;
        }
        //echo $sql;
        //echo $buyer_credit_weed_2;
        $nav_selected=3;	
	    $this->set('user_id',$user_id );
        //卖家信誉		
	    $this->set('seller_credit',$seller_credit );		
	    $this->set('seller_credit_num',$seller_credit_num );
	    $this->set('seller_credit_num_2',$seller_credit_num_2 );
	    $this->set('seller_credit_num_1',$seller_credit_num_1 );
	    $this->set('seller_credit_num_0',$seller_credit_num_0 );
	    $this->set('seller_percentage',$seller_percentage );		
		
	    $this->set('LastWeek',$LastWeek );		
	    $this->set('LastMonth',$LastMonth );			
	    $this->set('SexMonth',$SexMonth );			
	    $this->set('seller_credit_weed_2',$seller_credit_weed_2 );			
	    $this->set('seller_credit_month_2',$seller_credit_month_2 );	
	    $this->set('seller_credit_sex_2',$seller_credit_sex_2 );	
	    $this->set('seller_credit_before_2',$seller_credit_before_2 );	
	    $this->set('seller_credit_num_2',$seller_credit_num_2 );
		
	    $this->set('seller_credit_weed_1',$seller_credit_weed_1 );			
	    $this->set('seller_credit_month_1',$seller_credit_month_1 );	
	    $this->set('seller_credit_sex_1',$seller_credit_sex_1 );	
	    $this->set('seller_credit_before_1',$seller_credit_before_1 );	
	    $this->set('seller_credit_num_1',$seller_credit_num_1 );	

	    $this->set('seller_credit_weed_0',$seller_credit_weed_0 );			
	    $this->set('seller_credit_month_0',$seller_credit_month_0 );	
	    $this->set('seller_credit_sex_0',$seller_credit_sex_0 );	
	    $this->set('seller_credit_before_0',$seller_credit_before_0 );	
	    $this->set('seller_credit_num_0',$seller_credit_num_0 );

	    $this->set('seller_credit_weed',$seller_credit_weed );			
	    $this->set('seller_credit_month',$seller_credit_month );	
	    $this->set('seller_credit_sex',$seller_credit_sex );	
	    $this->set('seller_credit_before',$seller_credit_before );	
	    $this->set('seller_credit_num',$seller_credit_num );			
		//买家信誉
	    $this->set('buyer_credit',$buyer_credit);		
	    $this->set('buyer_credit_num',$buyer_credit_num );
	    $this->set('buyer_credit_num_2',$buyer_credit_num_2 );
	    $this->set('buyer_credit_num_1',$buyer_credit_num_1 );
	    $this->set('buyer_credit_num_0',$buyer_credit_num_0 );		
	    $this->set('buyer_percentage',$buyer_percentage );	

	    $this->set('buyer_credit_weed_2',$buyer_credit_weed_2 );			
	    $this->set('buyer_credit_month_2',$buyer_credit_month_2 );	
	    $this->set('buyer_credit_sex_2',$buyer_credit_sex_2 );	
	    $this->set('buyer_credit_before_2',$buyer_credit_before_2 );	
	    $this->set('buyer_credit_num_2',$buyer_credit_num_2 );
		
	    $this->set('buyer_credit_weed_1',$buyer_credit_weed_1 );			
	    $this->set('buyer_credit_month_1',$buyer_credit_month_1 );	
	    $this->set('buyer_credit_sex_1',$buyer_credit_sex_1 );	
	    $this->set('buyer_credit_before_1',$buyer_credit_before_1 );	
	    $this->set('buyer_credit_num_1',$buyer_credit_num_1 );	

	    $this->set('buyer_credit_weed_0',$buyer_credit_weed_0 );			
	    $this->set('buyer_credit_month_0',$buyer_credit_month_0 );	
	    $this->set('buyer_credit_sex_0',$buyer_credit_sex_0 );	
	    $this->set('buyer_credit_before_0',$buyer_credit_before_0 );	
	    $this->set('buyer_credit_num_0',$buyer_credit_num_0 );

	    $this->set('buyer_credit_weed',$buyer_credit_weed );			
	    $this->set('buyer_credit_month',$buyer_credit_month );	
	    $this->set('buyer_credit_sex',$buyer_credit_sex );	
	    $this->set('buyer_credit_before',$buyer_credit_before );	
	    $this->set('buyer_credit_num',$buyer_credit_num );

        if(empty($SHOP['shop_template'])){
		    $SHOP['shop_template'] = $this->isuservip ;
		}
        if(isset($privilege[11]) && $privilege[11]>0 ){
		    $SHOP['shop_template'] = $this->vipviews ;
		}		
		if($SHOP['shop_template']== $this->isuservip) {
		    $this->render('credit');	
		}else {
		    $this->render($SHOP['shop_template'].'/credit');		
		}		
	
	}
	
	public function productshow($goods_id) {
        //将游览的商品id放入cookie中，防刷新
		$visitPSPframeworkGoods = $this->C_session->read('_user_temp.visitPSPframeworkGoods');
        if($visitPSPframeworkGoods){
	        $remotegoods=explode(",",$visitPSPframeworkGoods);
	        $i=true;
	        foreach ($remotegoods as $v){
		        if($v==$goods_id){
			        $i=false;
		        }
	        }
	        if($i){
			    $this->C_session->write('_user_temp.visitPSPframeworkGoods', $visitPSPframeworkGoods.",".$goods_id);
				$query  = array();		
				$query['keyid'] = true;
				$safeData['goods'] = array('pv +'=>1);				
				$query['set'] = $safeData;	
				$query['conditions'] = array('goods.id'=>$goods_id);			
				POP::exe('[@@:UM goods][@@:QU save]',$query);	            
	        }

        }else{
	        $remoteip=Request::getclientIp();
			$this->C_session->write('_user_temp.visitPSPframeworkGoods', $remoteip.','.$goods_id);
			$query  = array();		
			$query['keyid'] = true;
			$safeData['goods'] = array('pv +'=>1);				
			$query['set'] = $safeData;	
			$query['conditions'] = array('goods.id'=>$goods_id);			
			POP::exe('[@@:UM goods][@@:QU save]',$query);	        
        }

		$query = array(
            'conditions' => array('goods.id'=>$goods_id,'goods.is_on_sale'=>1),
			'fields' => array('goods.*'), 
		);
        $goodsinfo = POP::exe('[@@:UM goods][@@:QU find:first]',$query);

		if(empty($goodsinfo)) {
		    $this->flash('该产品不存在！', Request::referer());
		}else {	
		if($goodsinfo['goods']['lock_flg']) {
		    $this->flash('此商品已被锁定！', Request::referer());
		}else {	
		$sub_category = array();
        if($goodsinfo['goods']['cat_id']) {
		    $cat = $goodsinfo['goods']['cat_id'];
			$query = array(
				'fields' => array('goodscategories.*'), 
			);
	        $arr = POP::exe('[@@:UM goodscategories][@@:QU find:all]',$query);
            if($arr) {
			    $index = 0;
                $cats  = array();				
                while (1)
                {
                    foreach ($arr AS $row)
                    {
                        if ($cat == $row['goodscategories']['id'])
                        {
                            $cat = $row['goodscategories']['parent_id'];
                            $cats[$index]['cat_id']   = $row['goodscategories']['id'];
                            $cats[$index]['cat_name'] = $row['goodscategories']['cat_name'];
                            $cats[$index]['url'] = Router::url('default/productlist1/?id='.$row['goodscategories']['id']);							
                            $index++;
                            break;
                        }
                    }
                    if ($index == 0 || $cat == 0)
                    {
                        break;
                    }
                }				
                $sub_category = array_reverse($cats);				
			}
			
		}
        //获得地区列表		
		$query = array(
            'conditions' => array('area.area_type' => 1),
			'fields' => array('area.*'), 
		);
		$area_list = POP::exe('[@@:UM area][@@:QU find:first]',$query);
		
        //获取商家信用值
        $shop_id = $goodsinfo['goods']['shop_id'];
        
		$query = array(
            'conditions' => array('credit.seller' => $shop_id),
			'fields' => array('credit.seller_credit'),
			'calculate'=>'sum' 
		);
		$query1 = array(
            'conditions' => array('credit.seller' => $shop_id,'credit.goods_id'=>$goods_id),
			'fields' => array('credit.seller_credit'),
			'calculate'=>'sum' 
		);
		$query2 = array(
            'conditions' => array('credit.seller' => $shop_id,'credit.goods_id'=>$goods_id),
			'fields' => array('credit.*'),
			'calculate'=>'count' 
		);		
	    $credit = POP::exe('[@@:UM credit][@@:QU find:calculate]',$query);
	    $credit_1 = POP::exe('[@@:UM credit][@@:QU find:calculate]',$query1);	    
	    $credit_2 = POP::exe('[@@:UM credit][@@:QU find:calculate]',$query2);	
		
        $credit['SUM(seller_credit)'] = intval($credit[0][0]['SUM(seller_credit)']);
        if( $credit['SUM(seller_credit)'] < 0)$credit['SUM(seller_credit)']=0;	
        $credit_1['SUM(seller_credit)'] = intval($credit_1[0][0]['SUM(seller_credit)']);
        if( $credit_1['SUM(seller_credit)'] < 0)$credit_1['SUM(seller_credit)']=0;
		
        $seller_credit = $credit['SUM(seller_credit)'];
		$goods_credit = $credit_1['SUM(seller_credit)'];	
		$goods_sell = $credit_2[0][0]['count'];			

		$query = array(
            'conditions' => array('integral.int_min <'=>($credit['SUM(seller_credit)']+1),'integral.int_max >'=>($credit['SUM(seller_credit)']-1)),
			'fields' => array('integral.*'), 
		);
        $credit_row = $integral = POP::exe('[@@:UM integral][@@:QU find:first]',$query);

	   if($integral){
            $integral = $integral['integral']['int_grade'];
        }else{
            $integral = 0;
        }	
	
		$query = array(
				'joins' => array(
				array('type'=>'left','table'=>'userrank','conditions'=>array('user.rank_id' =>'userrank.id')),					
				),
				'fields' => array('userrank.rank_name'),
				'conditions' => array('user.id' =>$shop_id),
			);	
	    $rank_name = POP::exe('[@@:UM user][@@:QU find:first]',$query);
	    
		$rank_name['rank_name'] = $rank_name[0]['b']['rank_name'];
		$query = array(
			'conditions' => array('goodsgallery.goods_id' => $goods_id),
			'order' => 'is_set desc', 
			'fields' => array('goodsgallery.*'), 
		);
		$gallery = POP::exe('[@@:UM goodsgallery][@@:QU find:all]',$query);
		
		$query = array(
			'conditions' => array('goodsattr.goods_id' => $goods_id), 
			'fields' => array('goodsattr.*'), 
		);
		$goods_attr = POP::exe('[@@:UM goodsattr][@@:QU find:all]',$query);
		
        $attr = array();
        $attr_ids = array();
        $attr_status = false;
        $attribute = array();		
        if($goods_attr) {
	        foreach($goods_attr as $key=>$value) {
		        $attr[$value['goodsattr']['attr_id']] = $value['goodsattr']['attr_values'];
		        $attr_ids[] = $value['goodsattr']['attr_id'];
	        }
			$query = array(
				'conditions' => array('attribute.id' => $attr_ids), 
				'fields' => array('attribute.*'), 
			);
		    $attribute_result = POP::exe('[@@:UM attribute][@@:QU find:all]',$query); 
	        
	        foreach($attribute_result as $value) {
		        $attribute[$value['attribute']['id']] = $value['attribute']['attr_name'];
	        }
	        $attr_status = true;
        }
		$query = array(
			'fields' => array('area.*'), 
		);
		$array = POP::exe('[@@:UM area][@@:QU find:all]',$query);
	    $areainfo = array();
	    foreach($array as $k=>$v) {
		    $areainfo[$v['area']['id']] = $v['area']['area_name'];
	    }	
        /* 显示支付方式 */
		$query = array(
				'joins' => array(
				array('type'=>'left','table'=>'payment','conditions'=>array('shoppayment.pay_id' =>'payment.id')),					
				),
				'fields' => array('payment.id','payment.pay_code','payment.pay_name'),
				'conditions' => array('shoppayment.shop_id' =>$shop_id,'shoppayment.enabled' =>1),
			);	
        $result = POP::exe('[@@:UM shoppayment][@@:QU find:all]',$query);

        $payment_info = array();
        if($result) {
	        foreach($result as $value) {
		        $temp = trim($value['payment']['pay_code'],' 0123456789');
		        $payment_info[$temp] = $value['payment']['pay_name'];
	        }
        }
		$shop_id = $goodsinfo['goods']['shop_id'];
		$query = array(
			'conditions' => array('shopinfo.id' => $shop_id), 
			'fields' => array('shopinfo.*'), 
		);
        $SHOP = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);        
		$SHOP = $SHOP['shopinfo'];


		$query = array(
			'conditions' => array('user.id' => $SHOP['user_id']), 
			'fields' => array('user.*'), 
		);
        $ranks = POP::exe('[@@:UM user][@@:QU find:first]',$query);        		
        $SHOP['rank_id'] = $ranks['user']['rank_id'];		
		$query = array(
			'conditions' => array('userrank.id' => $SHOP['rank_id']), 
			'fields' => array('userrank.*'), 
		);
        $user_rank = POP::exe('[@@:UM userrank][@@:QU find:first]',$query);                	
        $user_rank = $user_rank['userrank'];
		
        		
        $best_num = 8;
        $privilege = unserialize($user_rank['privilege']);
        if($privilege){
	        $best_num = $privilege[4];
        }		
		

        $header['title'] = $goodsinfo['goods']['goods_name']." - ".$SHOP['shop_name'];
        $header['keywords'] = $goodsinfo['goods']['goods_name'].','.$goodsinfo['goods']['keyword'];
        $header['description'] = strip_tags($goodsinfo['goods']['goods_intro']);

		$query = array(
            'conditions' => array('shopusercategorie.shop_id' => $shop_id),
            'order'=>'sort_order asc',
			'fields' => array('shopusercategorie.*'), 
		);
        $category_list = POP::exe('[@@:UM shopusercategorie][@@:QU find:all]',$query);


        $category_list_new = array();
        if(!empty($category_list)) {
	        foreach($category_list as $v) {
		        $category_list_new[$v['shopusercategorie']['id']]['id'] = $v['shopusercategorie']['id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_name'] = $v['shopusercategorie']['shop_cat_name'];
		        $category_list_new[$v['shopusercategorie']['id']]['parent_id'] = $v['shopusercategorie']['parent_id'];
		        $category_list_new[$v['shopusercategorie']['id']]['shop_cat_unit'] = $v['shopusercategorie']['shop_cat_unit'];
		        $category_list_new[$v['shopusercategorie']['id']]['sort_order'] = $v['shopusercategorie']['sort_order'];
	        }
        }		
        unset($category_list);
        $this->set('category_list_new',$category_list_new);		
		$query = array(
            'conditions' => array('shoprequest.user_id' => $shop_id),
			'fields' => array('shoprequest.*'), 
		);
        $SHOPREQUEST = POP::exe('[@@:UM shoprequest][@@:QU find:first]',$query); 
		$SHOPREQUEST = $SHOPREQUEST['shoprequest'];			
	    $this->set('SHOPREQUEST',$SHOPREQUEST);	

		$query = array(
            'conditions' => array('shoplinks.shop_id'=>$shop_id,'shoplinks.is_show'=>1),
            'order'=>'id desc',
            'limit'=>10,
			'fields' => array('shoplinks.*'), 
		);
        $shoplinks = POP::exe('[@@:UM shoplinks][@@:QU find:all]',$query); 
        $this->set('shoplinks',$shoplinks);	

		$query = array(
            'order' => ' id asc ',            
			'fields' => array('ensure.*'), 
		);
		$ensure = POP::exe('[@@:UM ensure][@@:QU find:all]',$query); 

        $ensureids =array();
        $userensure =array();		
        foreach($ensure as $key => $value) {
            $ensureids[$value['ensure']['id']] = $value['ensure']['id'];
        }

		$query = array(
            'conditions' => array('userensure.ensure_id' => $ensureids ,'userensure.user_id' => $shop_id),
			'fields' => array('userensure.*'), 
		);
		$temp = POP::exe('[@@:UM userensure][@@:QU find:all]',$query); 

        foreach($temp as $key => $value) {
            $userensure[$value['userensure']['ensure_id']] = $value['userensure'];
        }        		
        $this->set('ensure',$ensure);
        $this->set('userensure',$userensure);		
		
		$query = array(
            'conditions' => array('tag.goods_id' => $goods_id),
            'order'=>'short_order ASC,tag_num DESC',
            'limit'=>15,
            'group'=>'tag_name',
			'fields' => array('tag.*'), 
		);
		$tag_list = POP::exe('[@@:UM tag][@@:QU find:all]',$query); 		

	    foreach ($tag_list as $k=>$v){
		    $tag_list[$k]['url']="search.php?k=".urlencode($v['tag']['tag_name']);
			$query = array(
	            'conditions' => array('tag.tag_name' => $v['tag']['tag_name']),
				'fields' => array('tag.tag_name'),
				'calculate'=>'count' 
			);		
	        $tag_num=POP::exe('[@@:UM tag][@@:QU find:calculate]',$query);
            $tag_num = intval($tag_num[0][0]['num']);			
		    $tag_list[$k]['tag_num'] = $tag_num;
	    }				
	    $this->set('goodsinfo',$goodsinfo);
	    $this->set('gallery',$gallery );
	    $this->set('SHOP',$SHOP);
	    $this->set('areainfo',$areainfo );
	    $this->set('payment_info',$payment_info );		
	    $this->set('ranks',$ranks );
	    $this->set('sub_category',$sub_category );
	    $this->set('attr_status',$attr_status );		
	    $this->set('attr',$attr );		
	    $this->set('attribute',$attribute );
	    $this->set('seller_credit',$seller_credit );
	    $this->set('rank_name',$rank_name );
	    $this->set('goods_credit',$goods_credit );
	    $this->set('goods_sell',$goods_sell );
	    $this->set('page',intval(Request::get_args('page')));
        if(empty($SHOP['shop_template'])){
		    $SHOP['shop_template'] = $this->isuservip ;
		}

        if(isset($privilege[11]) && $privilege[11]>0 ){
		    $SHOP['shop_template'] = $this->vipviews ;
		}		
		if($SHOP['shop_template']== $this->isuservip) {
		    $this->render('productshow');	
		}else {
		    $this->render($SHOP['shop_template'].'/productshow');		
		}	
       }}
	}
	
	public function goodcredit() {
        $credit=array(
		"1"=>"好",
		"0"=>"中",
		"-1"=>"差",
	    );	
        $page = intval(Request::get_args('page'));
        $goods_id = intval(Request::get_args('good_id'));

        $url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $tb = array('model' => 'credit',
 					'joins' => array(					
					array('type'=>'left','table'=>'user','conditions'=>array('credit.buyer' =>'user.id')),					
					),       
				    'fields' => array("credit.id","credit.buyer_evaltime","credit.buyer_evaluate","credit.buyer_credit","credit.buyer_explanation","credit.buyer_exptime","user.id","user.username"),
					'conditions' => array('credit.goods_id'=>$goods_id , 'credit.buyer_evaltime <>'=>null),
					'order' => '',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
	    $result =  $this->fpage($tb); 

        $data ='';
		$k=0;
        foreach($result['data'] as $val) {
		    $k++;$y='';
			if($k%2==0)$y='evaluate-item2';
		    $data .='<div class="evaluate-item  '.$y.'">
                        <div class="evaluate-item-icon"><p>'.$credit[$val['buyer_credit']].'评</p></div>
                        <div class="evaluate-item-content">'.$val['buyer_evaluate'].'
                            <div class="evaluate-time">['.$val['buyer_evaltime'].']</div>
                        </div>
                        <div class="evaluate-item-person"><a href="#">买家：'.$val['user_name'].'</a></div>
                        <div class="evaluate-item-pinfo" style="width:180px;">'.$val['buyer_explanation'].'</div>
                        <div class="evaluate-item-operate"><span>'.($val['buyer_explanation']?$val['buyer_exptime']:'').'</span></div>
                        <div class="clear"></div>
                    </div>'	;	
		
		}
        $data  .=  '<div class="clear"></div>
                	<div class="page-box" style="margin-top:0px; background-color:#f9f8f8; text-align:right; padding-right:10px; _padding-top:5px;">
                        <a href="'.$result['page']['prevPage'].'" class="pre">上一页</a>
                        <span class="on">'.$result['page']['current']['num'].'</span>
                        <a href="'.$result['page']['nextPage'].'" class="next">下一页</a>
                        <span>共'.$result['page']['pageCount'].'页</span>
                    </div> ';		
        $this->ViewFrame($data,Request::$query['iframeID'],'',false);
	}
	
	public function goodrecord() {
        $page = intval(Request::get_args('page'));
        $goods_id = intval(Request::get_args('good_id'));

        $url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $tb = array('model' => 'ordergoods',
 					'joins' => array(
					array('type'=>'left','table'=>'orderinfo','conditions'=>array('ordergoods.order_id' =>'orderinfo.id')), 					
					array('type'=>'left','table'=>'user','conditions'=>array('user.id' =>'orderinfo.user_id')),					
					),       
				    'fields' => array("user.id","user.username","ordergoods.goods_id","ordergoods.goods_name","ordergoods.goods_price","ordergoods.order_num","orderinfo.shipping_time"),
					'conditions' => array('ordergoods.goods_id'=>$goods_id , 'orderinfo.order_status'=>3),
					'order' => '',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
	    $result =  $this->fpage($tb); 

        $data ='';
		$k=0;
        foreach($result['data'] as $val) {
		    $k++;$y='';
			if($k%2==0)$y='evaluate-item2';
		    $data .='<div class="evaluate-item  '.$y.'">
                        <div class="evaluate-item-icon"></div>
                        <div class="evaluate-item-content" style="width:400px;"><p>买家：'.$val['user_name'].'</p>
						商品名称:'.$val['goods_name'].'
                            <div class="evaluate-time">出价记录[￥'.$val['goods_price'].'元]</div>
                        </div>
                        <div class="evaluate-item-person">数量：'.$val['order_num'].'</div>
                        <div class="evaluate-item-pinfo">状态:成交</div>
                        <div class="evaluate-item-operate">成交时间:<span>'.($val['shipping_time']?$val['shipping_time']:'').'</span></div>
                        <div class="clear"></div>
                    </div>'	;	
		
		}
        $data  .=  '<div class="clear"></div>
                	<div class="page-box" style="margin-top:0px; background-color:#f9f8f8; text-align:right; padding-right:10px; _padding-top:5px;">
                        <a href="'.$result['page']['prevPage'].'" class="pre">上一页</a>
                        <span class="on">'.$result['page']['current']['num'].'</span>
                        <a href="'.$result['page']['nextPage'].'" class="next">下一页</a>
                        <span>共'.$result['page']['pageCount'].'页</span>
                    </div> ';		
        $this->ViewFrame($data,Request::$query['iframeID'],'',false);
	}	
	
	public function goodsaddcart() {
        header('Content-Type: text/html; charset='.Param::read('App.encoding'));	
        if(!$this->my->id) {
	        exit("-3");
        }	
        $goods_id = intval($_POST['id']);
        $num = intval($_POST['num']);
        if(!$goods_id || !$num) {
	        exit();
        }	
		$query = array(
				'fields' => array('goods.*'),
				'conditions' => array('goods.id' => $goods_id),
				);	
        $goods_info = POP::exe('[@@:UM goods][@@:QU find:first]',$query);

		if(!$this->C_session->read('_user_temp.cart')) {
		    $this->C_session->write('_user_temp.cart', array());
		}
        if($goods_info) {
	        if($num > $goods_info['goods']['goods_number']) {
		        exit('-2');
	        }
        } else {
	        exit(0);
        }
		$car=$this->C_session->read('_user_temp.cart');
        if ($car) {
	        if(isset($car[$goods_id])){
		        $car[$goods_id]['num']=$car[$goods_id]['num']+$num;
		        $car[$goods_id]['add_time']=date("Y-m-d H:i:s");
		        $this->C_session->write('_user_temp.cart', $car);
	        }
        }else{
	        $car[$goods_id]=array();
	        $car[$goods_id]['num']=$num;
	        $car[$goods_id]['add_time']=date("Y-m-d H:i:s");
	        $this->C_session->write('_user_temp.cart', $car);
        }	
		$query = array(
				'fields' => array('cart.*'),
				'conditions' => array('cart.goods_id' => $goods_id,'cart.user_id' => $this->my->id),
				);	
        $row = POP::exe('[@@:UM cart][@@:QU find:first]',$query);

        if($row) {
            $insert_array['cart'] = array(
	            'goods_number' => $row['cart']['goods_number']+$num,
            );	
			$query  = array();		
			$query['keyid'] = true;				
			$query['set'] = $insert_array;	
			$query['conditions'] = array('cart.id'=>$row['cart']['id']);			
			POP::exe('[@@:UM cart][@@:QU save]',$query);		
	        exit('-1');
        }
        $insert_array['cart'] = array(
	        'user_id' => $this->my->id,
	        'goods_id' => $goods_id,
	        'goods_number' => $num,
	        'add_time' => date("Y-m-d H:i:s"),
        );
		if(POP::exe('[@@:UM cart][@@:QU save]',$insert_array)){ 
		    echo "1";
	    }

		exit;
	}	
	public function shopfavorite() {
        header('Content-Type: text/html; charset='.Param::read('App.encoding'));	
        if(!$this->my->id) {
	        exit("-3");
        }
        $shop_id = intval($_POST['id']);			
        if(!$shop_id) {
	       exit();
        }		

		$query = array(
				'fields' => array('favorite.*'),
				'conditions' => array('favorite.shop_id' => $shop_id,'favorite.user_id' => $this->my->id),
				);	
 		$row = POP::exe('[@@:UM favorite][@@:QU find:first]',$query);
		
        if($row) {
	        exit('-1');
        }
		$query = array(
				'fields' => array('shopinfo.*'),
				'conditions' => array('shopinfo.id' => $shop_id),
				);	
 		$row = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);
	
        if(!isset($row['shopinfo']['id'])) {
	        exit('-2');
        }
        if($row['shopinfo']['user_id'] == $this->my->id) {
	        exit('-4');
        }		
		$temp = array();
		$temp['favorite']['user_id'] = $this->my->id;
		$temp['favorite']['shop_id'] = $shop_id;		
		$temp['favorite']['add_time'] =date("Y-m-d H:i:s",time()) ;
		if(POP::exe('[@@:UM favorite][@@:QU save]',$temp)){ 
			$query  = array();		
			$query['keyid'] = true;
			$safeData['shopinfo'] = array('favpv +'=>1);				
			$query['set'] = $safeData;	
			$query['conditions'] = array('shopinfo.id'=>$shop_id);			
			POP::exe('[@@:UM shopinfo][@@:QU save]',$query);		
            echo "1";
        }
		exit;
	}		
	public function goodsfavorite() {
        header('Content-Type: text/html; charset='.Param::read('App.encoding'));		
        if(!$this->my->id) {
	        exit("-2");
        }	
        $goods_id = intval($_POST['id']);
        if(!$goods_id) {
	       exit();
        }		


		$query = array(
				'fields' => array('favorite.*'),
				'conditions' => array('favorite.goods_id' => $goods_id,'favorite.user_id' => $this->my->id),
				);	
 		$row = POP::exe('[@@:UM favorite][@@:QU find:first]',$query);
	
        if($row) {
	        exit('-1');
        }
		$query = array(
				'fields' => array('goods.*'),
				'conditions' => array('goods.id' => $goods_id),
				);	
 		$row = POP::exe('[@@:UM goods][@@:QU find:first]',$query);
	
        if(!$row) {
	        exit('-2');
        }	
		$temp = array();
		$temp['favorite']['user_id'] = $this->my->id;
		$temp['favorite']['goods_id'] = $goods_id;		
		$temp['favorite']['add_time'] =date("Y-m-d H:i:s",time()) ;
		if(POP::exe('[@@:UM favorite][@@:QU save]',$temp)){
			$query  = array();		
			$query['keyid'] = true;
			$safeData['goods'] = array('favpv +'=>1);				
			$query['set'] = $safeData;	
			$query['conditions'] = array('goods.id'=>$goods_id);			
			POP::exe('[@@:UM goods][@@:QU save]',$query);			
            echo "1";
        }
		exit;
	}


	
}

?>